PHP会话没有启动登录表单

时间:2014-09-11 22:48:07

标签: php session session-variables

我的用户登录没有启动会话,因此用户无法登录。在第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
}

?>

3 个答案:

答案 0 :(得分:1)

<强>解决方案

通过谷歌搜索&#34; ipage session not working&#34;我发现了这个:http://www.ipage.com/knowledgebase/read_article.bml?kbid=600

  

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

     

session_save_path(&#34;您的主目录路径&#34; / cgi-bin / tmp);   在session_start();

     

查找&#34;您的主目录路径&#34;:登录PHP Scripting页面   用于主目录的实际路径。替换&#34;您的主目录   路径&#34;显示路径。将session_save_path设置为其中的目录   你的cgi-bin:上面的例子中的/ cgi-bin / tmp或另一个   目录,只要绝对路径正确。

问题是服务器上运行PHP的进程没有写入全局会话文件夹的权限。您应该按照您的虚拟主机提供的说明进行操作,如果这不能解决您的问题,我建议您在他们的支持系统上打开一张票。

旧的建议供将来参考:

由于某种原因,$ _SESSION在重定向后没有继续运行。 Here's一份可能有问题的清单,但这里有几个关键点需要检查:

  1. 检查您是否确实被重定向到&#34; nextlogin.php&#34;,即。 $count确实是1。我知道这听起来很愚蠢,但有时会发生这些错误。
  2. 检查您的浏览器是否启用了Cookie。
  3. 尝试将重定向更改为header('Location: http://myhost.com/hs/nextlogin.php');
  4. 确保您输出所有错误。可能会有一些线索说明为什么会话没有被保存。
  5. 除了这些建议,我还需要更多信息来提出其他可能的解决方案。

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

      

    使用error_reporting(E_ALL);

         

    ini_set(&#39; error_reporting&#39;,E_ALL);

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

答案 1 :(得分:0)

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

$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"';    
}

答案 2 :(得分:0)

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

<?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.