无法获取$ _SESSION变量 [英] unable to get $_SESSION variable

查看:199
本文介绍了无法获取$ _SESSION变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



登录.php 我有

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

我收到错误像
未定义的索引邮件其他用户登录,另一方面我可以以admin的身份登录。



我有一个登录表单,看看是什么这种类型的登录是这样的,并在此 connect.php 中通过该类型的会话,它检查它是什么类型的,然后继续它的工作良好,但不幸的是获得错误不能再修改。



登录表单是用户,管理员和代理人的一种表单,我可以以管理员身份登录,但我无法以其他方式登录$($ _ SESSION)
{
session_regenerate_id();
session_start();
}
@mysql_connect('localhost','root','')或死(SERVER中的ERROR);
@mysql_select_db('module')or die(ERROR IN DATABASE);

$ _SESSION ['start'] = time(); //采取现在登录时间

if(!isset($ _ SESSION ['expire'])){
$ _SESSION ['expire'] = $ _SESSION ['start'] + (60 * 60); //在30秒内结束一个会话
}
$ now = time(); //检查主页开始的时间

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');
}

}


//用户登录//
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的); < /脚本>;
}
}


//代理登录//
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); < /脚本>;
}
}
}
else {
header('location:./ login / login.php');
}

以某种方式我以其他方式获得错误页面也显示电子邮件
现在要做什么?

解决方案

会话逐步




  1. 在所有事情之前定义会话,没有输出应该在之前, NO OUTPUT

      ?php 
    session_start();
    ?>


  2. 在一个页面中设置会话,然后你有权限访问,例如 page 1.php

     <?php 
    //这是第1页和那么我们将使用从此页面定义的会话:
    session_start();
    $ _SESSION ['email] ='email@example.com';
    ?>


  3. 2.php 中使用和获取会话

     <?php 
    //在此页面中,我将使用会话:
    session_start();
    if($ _ SESSION ['email']){
    echo'您的电子邮件在这里! :)';
    }
    ?>


strong>:评论没有输出。






也许回答你的问题:



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


您的登录应该是这样的




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



In Log-in.php I have

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

I am getting error like undefined index email for other user log-in, at other side I can log-in as admin here..

I have a log-in form that see rather what kind of log-in is this and pass that type in session in this connect.php its check what kind of type it is and then proceed it was working well but unfortunately getting error can't modify anymore.

log-in form is one form for user, admin and agents, where I am able to log-in as admin but I am not able to login as other it shows error

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');
}

In one way I am getting error in other way page is also displaying email what to do now?

解决方案

Sessions Step By Step

  1. Defining session before everything, No output should be before that, NO OUTPUT

    <?php
    session_start();
    ?>
    

  2. Set your session in a page and then you have access in that, for example this is page 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. Using and Getting session in 2.php

     <?php
       //In this page I am going to use session:
      session_start();
      if($_SESSION['email']){
      echo 'Your Email Is Here!  :) ';
      }
     ?>
    

NOTE: Comments don't have output.


Maybe answer to your question:

I think you are not setting session, and because of that PHP doesn't know that session variable.

Your login should be like this

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

这篇关于无法获取$ _SESSION变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆