0

我对 php 相当陌生,并试图重新熟悉这些信息。我目前有一个登录脚本工作正常,直到我设置变量 - 它检查用户/密码很好,但是当它重定向到 echosession.php 时 $_SESSION['username']; 变量不回显。另外 - 检查用户是否登录以及注销。

include 'db_connect.php';
include 'functions.php';
session_start();
//user tabls
 $tbl_name="users";

// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = md5($mypassword);
$myusername = $mysqli->real_escape_string($myusername);
$log_sql="SELECT * FROM $tbl_name WHERE email='$myusername' and password='$mypassword'";
$log_result=$mysqli->query($log_sql);

  // Mysql_num_row is counting table row
  $log_count=$log_result->num_rows;

// If result matched $myusername and $mypassword, table row must be 1 row
if($log_count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

// Set username session variable
$_SESSION['username'] = $myusername;


header("location:echosession.php");
}
else {
echo "Wrong Username or Password";
}

如果我尝试像这样加载 echosession.php:

session_start();
print("hi");
echo $_SESSION["username"];
var_dump($_SESSION);

页面只是空的。

4

1 回答 1

-1

第一的

session_start(); //always in first line.  Before include/require functions.

第二。您可以为非 cookie 用户添加会话 ID

if (session cookie exists == true)
   header('location:echosession.php');
else
   header('location:echosession.php?'.session_name().'='.session_id());

您 100% 确定 $myusername 不为空?

检查它

var_dump($myusername); die(); //in first script before add to session

我想知道为什么您在第二个脚本中没有输出。看起来您关闭了会话问题和错误报告。

在脚本开始处插入

<?PHP 
ini_set( 'display_errors', 'On' ); 
error_reporting( E_ALL ); 
?>
于 2013-01-27T16:30:31.600 回答