Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
PHP Sessions Easy Question?
I'm feeling that I'm just having a brain fart right now, but this is driving me nuts! I'm simply trying to access a session variable from one page to the next. To test this out, I've made pages, test1.php and test2.php. The code for each is short and simple:
test1:
<?php
session_start();
$_SESSION['name'] = "default";
echo "<html><body>Name = " . $_SESSION['name'] . "<br /><a href = 'test2.php'>Next</a></body></html>";
?>
test2:
<?php
session_start();
echo "<html><body>Name = " . $_SESSION['name'] . "</body></html>";
?>
Now, test1 should by default display "Name = default" It does. On page 2, I'm looking for the same, but I'm getting "Name = "
I was hoping someone would be able to see what easy mistake I've made that I'm just not catching right now. Thanks for your help!
3 Answers
- VBALv 49 years agoFavorite Answer
Are you hosting this on a remote server, or running a local webserver on your own computer? If it's on a remote webhost, I expect the problem is that the folder used to save session data, as set by their default PHP installation, either doesn't exist, or you don't have write permission to it. You need to change it to a directory in your webspace. Most webhosts will provide a web-based front-end / op panel that you can use to edit your php.ini file. In that file, look for the setting called session.save_path.
For example, I used powweb as my webhost. When I FTP to it, my home directory is
/home/users/web/b1366/pow.MY_ID
In that base folder, I have an htdocs folder. That's my web document root.
/home/users/web/b1366/pow.MY_ID/htdocs
So using FTP, I make a new folder
/home/users/web/b1366/pow.MY_ID/sess
It's outside of my web document root, so it can't be accessed by the public. But it is in a location I can write to. I absolutely would not want to store my session data anywhere under the htdocs folder!
Then, I use the op panel to edit my php.ini file, and set
session.save_path = "/home/users/web/b1366/pow.MY_ID/sess"
Hope it helps!
- fouquetLv 44 years ago
the two cookies and consultation variables would be hacked, inspite of the easily shown fact that the former is plenty much less complicated to do. A cookie is clever, for reoccuring visits. Eg: computerized login (as quickly as you % some factor like, save me logged in continually). inspite of the easily shown fact that, somebody who's nicely-known with what the content and talk over with of the cookie sounds like, can create an comparable cookie with the neccessary ideas. I frequently use consultation based login. do now now no longer save the password, truly the username indoors the consultation variable. And on each internet site I learn if the consultation variable consists of the username, if now now no longer I redirect to the login internet site. no could % to desire to learn with the database many times. it could truly make adventure if the username/password would be agencies to changing very frequently... which i'm assuming, isn't the case.
- rbjollyLv 59 years ago
Try the following in place of your pages and see what you get:
Test1:
<?php
// *** Set SHOW_ERRORS to false for production.
defined('SHOW_ERRORS') ? NULL : define('SHOW_ERRORS', true);
// Setup error reporting.
error_reporting(E_ALL);
if (SHOW_ERRORS) {
ini_set('display_errors', '1');
} else {
ini_set('display_errors', '0');
}
$isSession = session_start();
if ($isSession) {
$_SESSION['name'] = "default";
echo "<html> <body> <p>Name = {$_SESSION['name']}</p> <p><a href = 'test2.php'>Next</a> </p> </body> </html>";
} else {
echo "<html> <body> <p>Session not started. </p> </body> </html>";
}
?>
Test2:
<?php
// *** Set SHOW_ERRORS to false for production.
defined('SHOW_ERRORS') ? NULL : define('SHOW_ERRORS', true);
// Setup error reporting.
error_reporting(E_ALL);
if (SHOW_ERRORS) {
ini_set('display_errors', '1');
} else {
ini_set('display_errors', '0');
}
$isSession = session_start();
if ($isSession) {
$name = 'None Given';
if (isset($_SESSION['name'])) $name = $_SESSION['name'];
echo "<html> <body> <p>Name = {$name} </p> </body> </html>";
} else {
echo("Session not started.");
}
?>