Session variables do not work php

Here is the code for my login page, where the login script authenticates the user and then redirects to the inbox using the header function.

<?php session_start(); include_once('config.php'); $user=htmlentities(stripslashes($_POST['username'])); $password=htmlentities(stripslashes($_POST['password'])); // Some query processing on database if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){ $_SESSION['loggedIn'] = 'yes'; header("Location:http://xyz/inbox.php?u=$id_user_fetched"); //echo 'Login Successful'; }else{ echo 'Invalid Login'; echo'<br /> <a href="index.html">Click here to try again</a>'; } }else{ echo mysqli_error("Login Credentials Incorrect!"); } ?> 

The inbox.php page looks like this:

 <?php session_start(); echo 'SESSION ='.$_SESSION['loggedIn']; if($_SESSION['loggedIn'] != 'yes'){ echo $message = 'you must log in to see this page.'; //header('location:login.php'); } //REST OF THE CODE ?> 

Now with the above code, inbox.php always shows the result: SESSION = you must be logged in to view this page. This means that either the session variable is not configured, or inbox.php cannot receive the session variable. Where am I going wrong?

+6
source share
8 answers
  • Make sure session_start(); called before calling any sessions. Thus, a safe bet would be to place it at the top of your page immediately after the opening of the <?php tag before anything else. Also make sure there are no spaces / tabs before the opening <?php tag.
  • After redirecting the header end the current script with exit(); (others also suggested session_write_close(); and session_regenerate_id(true) , you can also try them, but I would use exit(); ).
  • Make sure cookies are enabled in the browser that you use for verification.
  • Make sure register_globals off, you can check this in the php.ini file as well as using phpinfo() . Refer to this on how to disable it.
  • Make sure you did not delete or clear the session.
  • Make sure the key in the $_SESSION supergell $_SESSION not overwritten anywhere.
  • Make sure you are redirected to the same domain. Therefore, redirecting from www.yourdomain.com to yourdomain.com does not take the session forward.
  • Make sure the .php file extension (this happens!).

PHP session after redirect

+20
source

I had the same problem for a while, and it was very difficult for me to understand it. My problem was that I had a website that worked for a while, when the sessions worked correctly, and then everything suddenly broke.

Apparently your session_save_path (), for me this is / var / lib / php 5 /, should have 777 chmod permissions. I accidentally changed it, completely breaking the session.

To fix, just do sudo chmod -R 777 /var/lib/php5/ (or whatever your session_save_path ()) is on linux.

EDIT: changing the permission to 777 is not the best solution if you know the user you are running on your server. Run sudo chmod -R 700 /var/lib/php5/ and then sudo chown -R www-data /var/lib/php5/ so that the php user has access to this folder.

+7
source

Perhaps if your session path is not working correctly, you can try session.save_path(path/to/any folder); function as an alternative way. If this works, you can ask your hosting provider for a default error.

+2
source

Just talking to the hosting service, that was the problem at the end. he said: "Your session.save_path account has not been installed because a problem has occurred. I have set it up for you now."

And it works fine after that :)

+1
source

If you are using a script connection, be sure to use session_start(); in this connection, there were some problems before noticing this problem.

+1
source

Another important reason why sessions cannot work is played back with session cookie settings, for example. session cookie lifetime to 0 or other low values ​​due to a simple error or another developer for any reason.

 session_set_cookie_params(0) 
+1
source

I had a similar problem with the cookie domain:

  ini_set('session.cookie_domain', '.domain.com'); 

the domain was not configured correctly, so all sessions were ignored because the user cookie was never set correctly, hope this helps someone.

0
source

Today I ran into this problem. the problem is with $ config ['base_url']. I noticed htpp: //www.domain.com and http://example.com . to fix always set base_url at http://www.example.com

0
source

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


All Articles