1

I am Creating a Login System but Unable to access Session Variables on other pages... I have two php files - (1).check_login.php and (2). test.php

(1).check_login.php

<?php
include 'connection.php';

$tbl_name = "guests";

if(isset($_POST['login'])){
$mobile = mysqli_real_escape_string($conn,$_POST['mobile']);

$pass = mysqli_real_escape_string($conn,$_POST['password']);
$sel_guest = "select * from guests where MobileNumber='$mobile' AND Password='$pass'";

$result = mysqli_query($conn, $sel_guest);

$count = mysqli_num_rows($result);

if($count>0){

$_SESSION['umobile']=$mobile;

echo "<script>alert('".$_SESSION['umobile']."')</script>";

//echo "<script>window.open('index.php','_self')</script>";


}

else {

echo "<script>alert('Email or password is not correct, try again!')</script>";

}
echo "<script>alert('".$_SESSION['umobile']."')</script>";
}
?>

(2). test.php

<?php 
session_start();
 echo "alert(".$_SESSION['umobile'].")";
 echo session_status();
?>

Thanks in Advance...

  • `check_login.php` requires `session_start();` at the top of the page or else session variables don't work. – Rasclatt May 03 '17 at 05:52

1 Answers1

0

Always start a session if you are using session across the application/website.

Example

a.php

session_start();
$_SESSION['abc'] = 'cde';

b.php

session_start();
echo $_SESSION['abc'];

This should work if you are in the same domain.

Nikhil
  • 1,450
  • 11
  • 24