0
好吧,所以我讓用戶登錄,然後「如果成功」我將他們重定向到index.php我想從數據庫中顯示一些信息。會話在PHP不工作
我的用戶已驗證,我可以登錄,但我認爲我在會話中遇到了問題。
當我在index.php頁面調用它時,用戶名會話不顯示任何信息。
我是新來的PHP和學習,因爲我去。我已經花了兩天時間瀏覽本網站,以查找我的問題的答案,但無法找到任何真正有用的東西。
下面是代碼
checklogin.php
<?php
session_start();
ob_start();
include_once 'config.php';
// Connect to server and select databse.
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$db = new PDO('mysql:host='.$host.';dbname='.$db_name.';charset=utf8', $username, $password);
}
catch(Exception $e)
{
die('Error : ' . $e->getMessage());
}
// Define $myusername and $mypassword
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$stmt = $db->query("SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
// rowCount() is counting table row
$count = $stmt->rowCount();
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1){
// Register $myusername, $mypassword and print "true"
echo "true";
$_SESSION['username'] = 'myusername';
$_SESSION['password'] = 'mypassword';
}
else {
//return the error message
echo "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>Wrong Username or Password</div>";
}
ob_end_flush();
?>
的index.php
<?php
session_start();
if(!isset($_SESSION['username'])){
header("location:main_login.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/main.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
<div class="form-signin">
<div class="alert alert-success">You have been <strong>successfully</strong> logged in <?php echo $_SESSION['username']; ?>.</div>
<a href="logout.php" class="btn btn-default btn-lg btn-block">Logout</a> </div>
</div>
<!-- /container -->
</body>
</html>
我真的很感激任何幫助或鏈接文章,可以幫助。
由於
肖恩
您的會話已啓用嗎?使用phpinfo()檢查; –
嗨,這裏是我的phpinfo()必須說 - http://prntscr.com/e2n4bn –
所以是的,我會說這是啓用 –