php - 无法获取 $_SESSION 变量

标签 php session error-handling

this error
Log-in.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 中的 session 中传递该类型,它会检查它是什么类型,然后继续它运行良好,但不幸的是出现错误,无法再修改。

登录表单是用户、管理员和代理的一种表单,我可以在其中以管理员身份登录,但我无法以其他方式登录,它显示错误

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

最佳答案

类(class)循序渐进

  1. 在一切之前定义 session ,在此之前不应有输出,NO OUTPUT

    <?php
    session_start();
    ?>
    
  2. 在页面中设置您的 session ,然后您就可以访问该页面,例如这是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. 2.php

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

注意:评论没有输出。


也许可以回答你的问题:

我认为您没有设置 session ,因此 PHP 不知道该 session 变量。

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

关于php - 无法获取 $_SESSION 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577605/

相关文章:

PHP PDO 返回相关表数据

php - 拉拉维尔 : how to avoid time overlapping?

ASP.NET;几个 session 变量或一个 "container object"?

spring-boot - 为 spring 启动代码添加自定义异常的最佳方法

javascript - Puppeteer 失败时自定义错误消息

php - 如何在查询更新或未更新时获取错误消息 codeigniter

php - 调试 NotORM 调用结果为 "Use of undefined constant STDERR - assumed ' STDERR'”

Python requests.get 显示 404 而 url 确实存在

java - 如何在 session 中存储用户ID并在android中的另一个类中检索它?

php将json数据添加到mysql