PHP会话未启动登录表单


PHP session not starting login form

我有一个未启动会话的用户登录,因此用户无法登录。在第2页中,我尝试echo $_SESSION['email'];查看会话变量是否正在传递,但没有显示任何内容。

我仔细检查了phpmyadmin和数据库的格式,它的列是正确的,以及这些表单正在查找的数据。

编辑:所有代码都在hs文件夹中。在任一session_start(); 之前都没有空白

第1页:

    <?php
    session_start();
    include '../hs/connect.php';
$email = mysqli_real_escape_string($con, trim($_POST['email'])); 
$password = mysqli_real_escape_string($con, $_POST['password']);
 // echo $email  when i tested these variables they returned the correct values
 // echo $password
$sql="SELECT * FROM users WHERE email='$email' and password='$password'";
$result=mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if ($count==1){
$_SESSION['email'] = $email;
echo '<meta http-equiv="refresh" content="0;url=/hs/nextlogin.php">';
}
else{
echo '<meta http-equiv="refresh" content="0;url=/hs/hs.php">';
}
?>

上述重定向到的页面(第2页):

<?php
session_start();
if (!isset($_SESSION['email'])) {

die("Please login <a href='../hs/hs.php'>here</a>");  //this keeps appearing even though I entered the correct login data
}
?>

解决方案

通过谷歌搜索";ipage会话不工作";我发现:http://www.ipage.com/knowledgebase/read_article.bml?kbid=600

要运行PHP会话,请在任何PHP的顶部包含以下代码使用会话的脚本:

session_save_path("您的主目录路径"/cgi-bin/tmp);session_start();

要找到";您的主目录路径":登录PHP脚本页面获取主目录的实际路径。替换";您的主目录路径";其中显示了路径。将session_save_path设置为您的cgi-bin:cgi-bin/tmp(如上面的例子)或其他目录,只要绝对路径正确即可。

问题是在服务器上运行PHP的进程没有写入全局会话文件夹的权限。你应该遵循你的网络主机提供的指示,如果这不能解决你的问题,我建议你在他们的支持系统上开一张罚单。

供未来参考的旧建议:

由于某种原因,$_SESSION在重定向后没有结转。这里有一个可能出错的清单,但这里有几个关键点需要检查:

  1. 检查您是否确实被重定向到";nextlogin.php";,即$count实际上是1。我知道这听起来很傻,但有时这些错误也会发生
  2. 检查您的浏览器中是否启用了Cookie
  3. 尝试将重定向更改为header('Location: http://myhost.com/hs/nextlogin.php');
  4. 确保输出所有错误。可能有一些线索可以说明为什么没有保存会话

除了这些建议,我还需要更多的信息来想出其他潜在的解决方案。

编辑1:要只在这些页面上打开错误报告,请将这两行添加到页面顶部,就在php打开标记之后

error_reporting(E_ALL);

ini_set('error_reporting',E_ALL);

编辑2:正如barmar在评论中所指出的,请确保<?php之前的文件中绝对没有任何内容,甚至没有空格。这也可能是会话处理本身的问题。您是否对php.ini中的会话处理进行了任何修改?

关于限制可以使用此查询的结果的想法:

$sql="SELECT * FROM users WHERE email='$email' and password='$password' LIMIT 1";

关于重定向,你可以尝试使用这个:

if ($count==1){
$_SESSION['email'] = $email;
  header('Location: "hs/nextlogin.php"';
}
else{
  header('Location: "hs/hs.php"';    
}

要正确调试,请尝试以下操作:

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set('error_reporting', E_ALL);
    ini_set("display_errors","1");
    ini_set("register_globals", "Off");
    include '../hs/connect.php';
    //...
    //the  rest of code here.