PHP Session Not working and not giving any error -


in login.php have

<?php   require_once('connection.php'); $loginerr = ""; if(isset($_post['login'])) {   $email = mysqli_real_escape_string($conn,$_post['email']);   $pass = mysqli_real_escape_string($conn,$_post['password']);   $sel_user = "select * register email='$email' , password='$pass'";   $run_user = mysqli_query($conn, $sel_user);   $check_user = mysqli_num_rows($run_user);   if($check_user>0){     $_session['email'] = $email;     header('location: '.'index.php');   }   else {     $loginerr = "email or password not correct, try again!";   } } ?> 

basically if username , password matches creates session "email" , redirects index page.

in navbar common pages index , other. have

<ul class="nav navbar-nav navbar-right">  <?php if(!isset($_session['email'])) { ?>  <li><a href="login">login</a></li>     <li><a href="register.php">register</a></li>     <?php } else { ?> <li><a href="logout.php">logout</a></li>    <?php } ?> </ul> 

basically if there no session email login , logout shown , if session email set logout shown.

but showing login , register button if user logged in or logged out.

my logout page

<?php session_start(); session_unset($_session['email']); session_destroy(); header("location: index.php"); ?> 

i trying show logout button when user logged in , login, register button when not. not able find error in code , doesn't give error. showing login register button always.

thanks.

edit 1:

forgot mention session start in connection.php file

edit 2:

session_start(); have put on first line of login page , homepage same problem

edit 3: when var dump session after session set session value of email when var dump in homepage undefined index: email

you have start_session(); in connection.php. or can start here this...

<?php  require_once('connection.php'); $loginerr = ""; if(isset($_post['login'])) {   $email = mysqli_real_escape_string($conn,$_post['email']);   $pass = mysqli_real_escape_string($conn,$_post['password']);   $sel_user = "select * register email='$email' , password='$pass'";   $run_user = mysqli_query($conn, $sel_user);   $check_user = mysqli_num_rows($run_user);   if($check_user>0){     session_start();     $_session['email'] = $email;     header('location:index.php');   }   else {     $loginerr = "email or password not correct, try again!";   } } ?> 

and logout.php must this...

<?php //session_start();  no need start here session_unset($_session['email']); session_destroy(); header("location: index.php"); ?> 

does not start_session @ home page.because session started @ once. , try following code.

<ul class="nav navbar-nav navbar-right">  <?php if(isset($_session['email'])) { ?>   <li><a href="logout.php">logout</a></li>   <?php } else { ?> <li><a href="login">login</a></li>     <li><a href="register.php">register</a></li>     <?php } ?> </ul> 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -