我試圖拋棄「歡迎,用戶名!」 ,但它總是推遲「不工作」。 我該如何解決這個問題?PHP用戶名歡迎信息無法正常工作
這是index_l.php頁面內容:
<?php
if(isset($_SESSION['username']) && $_SESSION['username']) {
echo "Welcome, ".$_SESSION['username']."!";
}
else{
echo "not working";
}
?>
這是登錄腳本:
<?php
include('connect.php');
// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM members WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
// Register $username, $password and redirect to file "login_success.php"
$_SESSION['username'];
$_SESSION['loggedin'] = true;
header("location:index_l.php");
}
else {
echo "Problem";
}
?>
當然要裝入'在session_start();'對?啊,知道了。 ;-) –
你甚至開始會議嗎?將'session_start();'添加到頂部的文件中。那麼所有的文件使用'$ _SESSION'。 –
所有你真正需要的是'if(isset($ _ SESSION ['username'])){...}'除非你已經將這個變量賦值給一個特定的名字或行等等。正如'if(isset($ _ SESSION ['username'])&& $ _SESSION ['username'] ==「你」){...}' –