php会话变量在第一次尝试时未存储,从第二次尝试起正常工作

时间:2014-01-30 23:17:15

标签: php session variables

我正在存储会话变量以进行有效登录并重定向到第二页,但在第二页中,当我第一次尝试登录时(删除cookie和浏览历史记录时),会话变量未设置。从第二次登录尝试,代码工作正常。请告知这里出了什么问题。 。下面是第一页和第二页的php代码

第一页

<?php

session_start(); 

$i  = 0;

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(empty($_POST['userid']) ||
        empty($_POST['password']) 
        ) 

   {
         echo "<script> alert('Please fill all the fields '" .$i ."); </script> ";
   }
   else
   {
   $x = $_POST['userid'];
   $y = $_POST['password'];

    $con=mysqli_connect('credentials. . ');

    $sql="SELECT class as cla, section, rollno
            FROM `parent`
            WHERE userid = '$x'
            AND password = '$y'     ;
        ";
    // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        $query = mysqli_query($con,$sql);

        $row = mysqli_fetch_array($query);

        if (mysqli_num_rows($query) > 0)
        {
            $i = 1;
            $_SESSION['user'] = $x;
            $_SESSION['pass'] = $y;
            $_SESSION['cla'] = $row['cla'];
            $_SESSION['sec'] = $row['section'];
            $_SESSION['rollno'] = $row['rollno'];

            header("Location: http://www.welcome.co.in/parent/studetails.php"); 
            exit;
        }
        else
        {
            echo "<script> alert('Invalid credentials, please contact school Admin '); </script> ";
        }
    }   

}

?>

第二页php代码:

<?php

session_start(); 

if (!isset($_SESSION['user']) OR !isset($_SESSION['pass']))
{


header("Location: http://www.welcome.co.in/parent");

}

else

   {

   $x = $_SESSION['user'];
   $y = $_SESSION['pass'];
   $cla = $_SESSION['cla'];
   $section = $_SESSION['sec'];
   $rollno = $_SESSION['rollno'];   


    $con=mysqli_connect('logincredentials. . ');

    $sql="SELECT query;
            ";

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query = mysqli_query($con,$sql);

     // Loop through the query results, outputing the options one by one
    while ($row = mysqli_fetch_array($query)) 
    {
        some output here. . . 
    }
}
?>

0 个答案:

没有答案