无法获得$ _SESSION变量

时间:2014-12-20 06:16:51

标签: php session error-handling

this error
登录.php 我有

  $email=$_POST['email']; 
  $pass=$_POST['pass'];
  $_SESSION['type'] = 'user';
  $_SESSION['email'] = $email; 
  $_SESSION['pass']=$pass; 
  header('location:./connect.php');

我得到的错误就像 undefined index email用于其他用户登录,另一方面我可以在此处以管理员身份登录..

我有一个登录表单,看看这是什么类型的登录,并在此 connect.php 的会话中传递该类型,检查它是什么类型,然后继续它工作得很好,但遗憾的是,错误无法再修改。

登录表单是用户,管理员和代理的一种形式,我可以以管理员身份登录,但我无法登录,因为其他显示错误

if(empty($_SESSION))
{
   session_regenerate_id();
   session_start();
}
@mysql_connect('localhost','root','') or die("ERROR in SERVER");
@mysql_select_db('module') or die("ERROR IN DATABASE");

    $_SESSION['start'] = time(); // taking now logged in time

    if(!isset($_SESSION['expire'])){
        $_SESSION['expire'] = $_SESSION['start'] + (60* 60) ; // ending a session in 30 seconds
    }
    $now = time(); // checking the time now when home page starts

    if($now > $_SESSION['expire'])
    {
        session_destroy();

    }

if(!empty($_SESSION['type']))
{
$email = $_SESSION['email'];
$pass = $_SESSION['pass'];
$type = $_SESSION['type'];


// admin login //
if($type == 'admin'){




$admin = mysql_query("SELECT * FROM `admin` WHERE `email` ='$email' ");
$res = mysql_fetch_array($admin);
if($email == "" || $pass == "" || $email != $res['email'] || $pass != $res['pass'])
{
    header('location:./login/login.php');
}

}


// user login //
if($type == 'user')
{
     $email = $_SESSION['email'];
     $pass = $_SESSION['pass'];
     $type = $_SESSION['type'];
    $user = mysql_query("SELECT `id` FROM `users` WHERE `email`='$email' AND `status`='1'");
    $useres = mysql_fetch_array($user);
   // $trail = $useres['date'];
  //  $time = explode("/",$trail);


    if($email != $useres['email'] || $pass != $useres['pass'])

    {
        echo mysql_error();
//        header('location:./login/login.php');
    }

    else if($pass = $useres['pass']){
//        echo '<script> location.replace("./user.php"); </script>';
    }
}


// agent login //
if($type == 'agent')
{

    $email = $_SESSION['email'];
     $pass = $_SESSION['pass'];
     $type = $_SESSION['type'];
    $agent = mysql_query("SELECT `id` FROM `sale_agents` WHERE `email`='$email'");
    $agentres = mysql_fetch_array($agent);
    if($email != $agentres['email'] || $pass != $agentres['pass'])
    {
        header('location:./login/login.php');

    }
    else if($pass = $agentres['pass']){
   //     echo '<script> location.replace("./agent.php"); </script>';
    }
}
}
else{
    header('location:./login/login.php');
}

在某种程度上,我以其他方式收到错误页面也显示电子邮件 现在该做什么? enter image description here

3 个答案:

答案 0 :(得分:8)

逐步进行会话

  1. 在所有内容之前定义会话,之前不应输出 NO OUTPUT

    <?php
    session_start();
    ?>
    
  2. 在一个页面中设置您的会话,然后您可以访问该页面,例如页面1.php

    <?php
       //This is page 1 and then we will use session that defined from this page:
        session_start();
        $_SESSION['email]='email@example.com';
    ?>
    
  3. 2.php

    中使用和获取会话
     <?php
       //In this page I am going to use session:
      session_start();
      if($_SESSION['email']){
      echo 'Your Email Is Here!  :) ';
      }
     ?>
    
  4. 注意 :评论没有输出。


    也许回答你的问题:

    我认为你没有设置会话,因为PHP不知道会话变量。

      

    您的登录信息应该是这样的

      <?php
      session_start();
      $email=$_POST['email']; 
      $pass=$_POST['pass'];
      $_SESSION['type'] = 'user';
      $_SESSION['email'] = $email; 
      $_SESSION['pass']=$pass; 
      header('location:./connect.php');
      ?>
    

答案 1 :(得分:0)

在login.php中设置此代码:

session_start();
$email = $_POST['email'];
$pass = $_POST['pass'];
$_SESSION['type'] = 'user';
$_SESSION['email'] = $email;
$_SESSION['pass'] = $pass;
header('location:./connect.php');

这个在connect.php中:

session_start();
@mysql_connect('localhost', 'root', '') or die("ERROR in SERVER");
@mysql_select_db('module') or die("ERROR IN DATABASE");
$_SESSION['start'] = time(); // taking now logged in time
if (!isset($_SESSION['expire'])) {
    $_SESSION['expire'] = $_SESSION['start'] + (60 * 60);
}
$now = time(); // checking the time now when home page starts
if ($now > $_SESSION['expire']) {
    session_destroy();
    header('location:./login/login.php');
}
if (isset($_SESSION['email']) && !empty($_SESSION['email']) &&
        isset($_SESSION['pass']) && !empty($_SESSION['pass']) &&
        isset($_SESSION['type']) && !empty($_SESSION['type'])) {

    $email = $_SESSION['email'];
    $pass = $_SESSION['pass'];
    $type = $_SESSION['type'];

    // admin login //
    if ($type == 'admin') {
        $admin = mysql_query("SELECT * FROM `admin` WHERE `email`='$email'");
        $res = mysql_fetch_array($admin);
        if ($email == "" || $pass == "" || $email != $res['email'] || $pass != $res['pass']) {
            session_destroy();
            header('location:./login/login.php');
        }
    }

    // user login //
    if ($type == 'user') {
        $user = mysql_query("SELECT `id` FROM `users` WHERE `email`='$email' AND `status`='1'");
        $useres = mysql_fetch_array($user);
        // $trail = $useres['date'];
        //  $time = explode("/",$trail);
        if ($email != $useres['email'] || $pass != $useres['pass']) {
            session_destroy();
            header('location:./login/login.php');
        }
    }

    // agent login //
    if ($type == 'agent') {
        $agent = mysql_query("SELECT `id` FROM `sale_agents` WHERE `email`='$email'");
        $agentres = mysql_fetch_array($agent);
        if ($email != $agentres['email'] || $pass != $agentres['pass']) {
            session_destroy();
            header('location:./login/login.php');
        }
    }
} else {
    header('location:./login/login.php');
}

我希望这会对你有所帮助。

答案 2 :(得分:0)

session_start();

$_SESSION["user"] = $result['full_name'];
$_SESSION["user_id"] = $result['user_id'];
$_SESSION["website_type"] = $result['user_id'];

//这是我的代码... //我无法访问其他页面..

//第二页enter code here  session_start();  // print_r($ _ SESSION);

    echo $_SESSION['user'];
    echo $_SESSION['user_id'];
    echo $_SESSION['website_type'];