Failed to get $ _SESSION variable

this error
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 get an error like undefined index email for another user login, on the other hand I can log in as administrator here.

I have a login form that sees what kind of login it is and pass this type in a session in this connect.php to check what type it is and then continue it worked fine, but unfortunately getting an error can no longer be changed.

log-in form is one form for user, administrator and agents where I can log in as administrator, but I can not log in because it shows an 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'); } 

Unilaterally, I get an error message on another page, email is also displayed, what should I do now? enter image description here

+5
source share
3 answers

Sessions Step by Step

  • The definition of the session in front of everything, before the exit should not be, NO EXIT

     <?php session_start(); ?> 
  • Define your session on the page, and then you have access to it, 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 '; ?> 
  • Using and getting a 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 are not displayed.


Perhaps the answer to your question is:

I think you are not setting up a session, and because of this, PHP does not know this 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'); ?> 
+8
source

Install this code in login.php:

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

And this one in 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'); } 

Hope this helps you.

0
source

session_start ();

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

// this is my code ... // I can’t access on another page ..

// second page enter code here session_start (); // print_r ($ _ SESSION);

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

Source: https://habr.com/ru/post/1209590/


All Articles