Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
53
Q:

arrays - PHP Session variables are not passing to a second page even if it has **session_start()** on both pages

Hey everyone,

I'm having a little trouble with passing session variables between two pages in PHP. I have `session_start()` on both pages, so I'm not sure why the variables aren't being passed.

Here's the scenario: on the first page, let's call it `page1.php`, I have an input form where the user can enter some data. When they submit the form, the data gets stored in session variables:

```php
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
```

Then, on the second page, `page2.php`, I also have `session_start()`, but when I try to access the session variables set on `page1.php`, they are empty:

```php
session_start();
echo $_SESSION['name']; // Empty
echo $_SESSION['email']; // Empty
```

I've checked that the values are being properly stored in the session variables on `page1.php` by echoing them out on that page, and they are showing up correctly. So why aren't they being passed to `page2.php`?

I've double-checked that I have `session_start()` at the beginning of both pages and there are no other errors or warnings. Is there something else I need to do in order for the session variables to be accessible on `page2.php`?

I appreciate any help or insights you can provide. Thanks in advance!

All Replies

luna00

Hey there,

I've encountered a similar issue in the past, and it turned out that the problem was related to how I was navigating between the pages. When moving from `page1.php` to `page2.php`, I was using a regular anchor tag `<a href="page2.php">`, which was causing the session variables to not be passed along.

To resolve this, I switched to using `session_write_close()` before the redirect. Here's how I did it:

php
// On page1.php after setting the session variables
session_write_close();
header('Location: page2.php');
exit();


By calling `session_write_close()` before the redirect, it ensures that the session data is saved and available on `page2.php`. Then, on `page2.php`, I just need to start the session again with `session_start()` to access the session variables.

I hope this helps! Let me know if you have any further questions.

max62

Hey,

I've faced a similar issue with session variables not being passed between pages in PHP, even though I had `session_start()` on both pages. After trying various solutions, I discovered that the problem was related to a configuration setting in the PHP.ini file.

In my case, the `session.cookie_domain` setting in PHP.ini was not properly configured. By default, its value was set to an empty string, which caused the session variables to not be passed between pages.

To fix this, I had to set the `session.cookie_domain` to the correct domain value. For example:

ini
session.cookie_domain = ".example.com"


Make sure to replace `.example.com` with your actual domain. This change ensures that the session cookie is properly shared between pages within the same domain.

After making this adjustment, my session variables started being passed correctly to the second page (`page2.php` in your case).

I hope this helps you resolve your issue as well. Give it a try and let me know if you have any further questions!

New to LearnPHP.org Community?

Join the community