Taranaki Blaze
Taranaki Blaze

Reputation: 43

PHP $_SESSION[''] only working on some pages

I think I have set everything up right. But $_SESSION variables load fine on some pages, and not at all on others. The problems only came about when I rearranged the directories to make the URL neater. Ie /account.php became /account/index.php. All pages are set to load this code at the start:

ob_start();
ini_set('display_errors',8191);
session_set_cookie_params(1800, "./" , "www.example.com", true, true);
session_save_path("/home/users/web/example/writeable-directory/");
session_start();
echo($_SESSION['username']);

Most work right, but two pages do not.

When I use the following code, it will echo test on all pages.

ob_start();
ini_set('display_errors',8191);
session_set_cookie_params(1800, "./" , "www.example.com", true, true);
session_save_path("/home/users/web/example/writeable-directory/");
session_start();
$_SESSION['username'] = "test";
echo($_SESSION['username']);

Upvotes: 0

Views: 130

Answers (2)

Andy  Gee
Andy Gee

Reputation: 3335

Use session_start(); before any output to the browser. It's advisable to use session_start() as the first line of the file.

If it's still not working there's a possibility you have not set the correct keys. Check the output of this.

<?
session_start();
echo '<pre>';
print_r($_SESSION);
echo '</pre>';

Upvotes: 0

Taranaki Blaze
Taranaki Blaze

Reputation: 43

Ended up needing to change

session_set_cookie_params(1800, "/home/users/web/example/unix-directory/" , "www.example.com", true, true);

Thanks for the help though :)

Upvotes: 1

Related Questions