Report this

What is the reason for this report?

doesn't work php session

Posted on January 8, 2015
<?php 
session_start();

$_SESSION["name"] = "sami"; 
echo $_SESSION["name"]; 
?>

then;

<?php 
session_start();

echo $_SESSION["name"]; 
?>

session write but session doesn’t read.

chmod 777 /var/php5/sess*

but it doesn’t work.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This question was answered by @JonsJava:

Let’s clean this up a bit.

In file1.php, add this:

<?php
session_start();
$_SESSION['name'] = "sami";
header("location:file2.php");
exit();
?>

In file2.php

<?php
session_start();
echo "Your \$_SESSION['name'] var is ".$_SESSION['name'];
exit();
?>

View the original comment

The PHP code for setting and accessing session variables appears to be correct. If you’re able to write to the session but not read from it in subsequent requests, the issue might not be with the code itself but with the server configuration or session handling.

Here are a few things to check and try:

  1. Session Save Path: Ensure that the session save path configured in php.ini is correct and writable by the web server user. You’ve mentioned /var/php5/sess* but it could be different depending on your server configuration or PHP version.

  2. Permissions: chmod 777 is generally a bad practice because it allows any user to read and write to the session files, which is a significant security risk. Instead, the directory should have permissions that allow the web server user (like www-data for Apache on Ubuntu) to read and write files. You can usually achieve this with:

sudo chown -R www-data:www-data /path/to/session/directory
sudo chmod -R 770 /path/to/session/directory

Replace /path/to/session/directory with the actual session save path.

  1. Session Configuration: Check your php.ini file for the session settings. Look for session.save_handler and session.save_path to ensure they are set correctly. If you have access to the php.ini file, it is typically located in /etc/php/[version]/apache2/php.ini or a similar path depending on your PHP version and server setup.

  2. Cookies: PHP sessions rely on a cookie to keep track of the session ID between requests. Make sure cookies are enabled in the browser and that nothing in your code or server configuration is clearing or invalidating the session cookie.

  3. Session Start: Make sure session_start(); is called before any output is sent to the browser. Having any HTML or even whitespace before session_start(); can cause session initialization to fail.

  4. Session ID: Ensure that the session ID is not being regenerated unintentionally on every request, as this would cause the session data to not persist.

  5. Error Reporting: Enable error reporting in PHP to check for any warnings or errors that could be affecting session handling:

error_reporting(E_ALL);
ini_set('display_errors', 1);
  1. Session Data Serialization: There could be issues with how session data is being serialized and deserialized. Look for any messages in your PHP error logs that relate to session data handling.

  2. PHP Version: Make sure your PHP version is up-to-date or at least properly supported. Older versions may have bugs that affect session handling.

  3. Session Cleanup: Ensure that automatic session cleanup is not happening too frequently, which can cause sessions to expire unexpectedly.

  4. Debugging: As a debugging step, try using session_write_close() after setting session variables to force the session data to be written immediately.

If after checking all these points you still face issues, it could be helpful to look into the server logs for any session-related errors or warnings. Also, consider whether there are any other frameworks or scripts that might be interfering with session management.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.