Installing $ _SESSION does not work on localhost using XAMPP

I set up my session like in my PHP code: $_SESSION['user_id'] = $login; , and this seems to work just fine while downloading to my server and transferring to different pages, however, when I test the website on my local machine, the session seems to be disconnected immediately after exiting the script where it is installed.

I am testing my local machine with XAMPP and the exact same code.

I am not sure why this problem arises and would be very grateful for any useful answer.

An example that does not work:

 $_SESSION['user_id'] = $login; echo '<META HTTP-EQUIV="refresh" content="0;URL=../home.php">'; 

EDIT 1:

Here is my entire function in which I log in (this is part of the class):

 public function loggingIn(){ session_start(); $db = new Database('localhost','root','','userdata'); $userFunctions = new Users($db); $username = $_POST['usernameInput']; $password = $_POST['passwordInput']; if(empty($username) || empty($password)){ $this->errors['u&p'] = 'Please Enter Your Username AND Password'; $this->printE(); } elseif($userFunctions->user_exists($username)===false){ $this->errors['nm'] = 'That Username/Password Combination Is Not Valid'; $this->printE(); } else { $login = $userFunctions->login($username, $password); if($login === false){ $this->errors['nm'] = 'That Username/Password Combination Is Not Valid'; echo 'Login Failed!'; } else { if(!$userFunctions->economyTableExistsForUser($login)){ $userFunctions->createEconomyTableForUser($login); } if(!$userFunctions->schoolTableExistsForUser($login)) { $userFunctions->createSchoolTableForUser($login); } $_SESSION['user_id'] = $login; echo $_SESSION['user_id']; // working fine header('Location: ../home.php'); } } } 
+7
php session localhost
source share
8 answers

Suppose your home machine does not have:

session.autostart = On

in php.ini , while your other machine is explicitly.

Make sure you install in your PHP code:

session_start();

PHP: session_start - manual

If you do not, your session will not start if my first hypothesis is not true.

In addition, you should check the PHP version on your local host and settings in php.ini. Verify that the directory where the session files are stored can be written, and that the session files do exist.

EDIT 1:

Better use a PHP redirect solution:

 header("Location: /"); exit(); 

EDIT 2:

PHP has functions that change HTTP headers. Some of them:

EDIT 3:

Line breaks and spaces can be a problem, but there are also invisible character sequences that can trigger Warning: Cannot modify header information , like the UTF-8 Specification (Byte-Order-Mark) . Try saving the file again, making sure it is saved as UTF-8 (no BOM) .

EDIT 4:

To properly debug your code, make sure that PHP displays all warnings and notifications. Run it before the session starts:

 ini_set('display_errors', -1); 

EDIT 5:

You should also check the session.gc_maxlifetime setting:

session.gc_maxlifetime
session.gc_maxlifetime indicates the number of seconds after which data will be considered garbage and cleared. Garbage collection occurs during the start of the session.

EDIT 6:

To view spaces in Sublime Text, edit the settings:

 // Set to "none" to turn off drawing white space, "selection" to draw only the // white space within the selection, and "all" to draw all white space "draw_white_space": "selection", 

You can set it in Settings-> Default Settings. If you change your user settings "Settings" → "Settings" - "User" and add the line as shown below:

 { "font_size": 10, "draw_white_space": "all" } 

Also make sure that it displays all other special characters to properly debug your code!

EDIT 7:

Finally, try adding session_write_close(); right before the redirect.

EDIT 8:

Install PHP into your file, before the session_start(); directive session_start(); , session.save_path = "/home/username/tmp"; . Create tmp dir outside of public_html. Make sure tmp dir has chmod 770 and is created with the same user / group privileges. Run ls -lsa in your home directory to check if the new directory has the same user / group as other directories, for example public_html. If not, make sure you change the administrative permissions to tmp by running chown username:groupname /home/username/tmp .

+7
source share

Quick update. I found this answer very useful, however, note that when setting XAMPP, the php.ini entry for session.autostart = On is slightly different. I found the following:

; Initialize the session when the request starts. ; http://php.net/session.auto-start session.auto_start = 0

I changed this to:

; Initialize the session when the request starts. ; http://php.net/session.auto-start session.auto_start = 1

+3
source share

Make sure session cookies are enabled in php.ini ( xamp\php\php.ini ).

 session.use_cookies=1 
+2
source share

In your php.ini check:

+1
source share

If you are using PHP built into .htm / .html documents, you need to make sure that Apache can handle them accordingly. This is done by editing httpd.conf and adding a line

 AddType application/x-httpd-php .html .htm 

to section <IfModule mime_module>

+1
source share

I struggled with this problem, but decided ...

In /opt/lampp/etc/php.ini try changing this line:

 session.save_path = "/var/lib/php/session" 

For this:

 session.save_path = "/opt/lampp/var/session" 

Note. You may need to create a "session" folder in /opt/lampp/var

+1
source share
 Session Issue Fixed in Xampp 7.1.6 with following Changes in php.ini Line #1403 set: session.auto_start = 1 
0
source share

Enabling session cookies in php.ini (C: \ xamp \ php \ php.ini).

EDIT

 session.use_cookies=0 

TO

 session.use_cookies=1 

And don't forget to restart xampp

0
source share

All Articles