PHP用户名欢迎消息无效

时间:2014-05-24 16:11:46

标签: 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";
}
?>

2 个答案:

答案 0 :(得分:4)

您的代码无效,因为$_SESSION['username']没有值:

$_SESSION['username'];

您可能希望将其设置为$username

的值
$_SESSION['username'] = $username;

答案 1 :(得分:0)

会话变量没有值。

$_SESSION['username'] 

此外,

你必须添加

session_start();

在文件index_l.php和登录脚本

的最开头