Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach() Error on Session Values

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I have a little diagnostics script that fetches server, session and cookie info and it works well on my live server. It also worked well on my development system until I switched recently to Linux (Ubuntu 8.10) where the line that gives the session values generates an error.

Code:
<html>
<head>
</head>
<body>
<?php
$s = "<h4>Server Values</h4>";
foreach($_SERVER as $key=>$value)
$s = $s . "<b>" . $key . "</b> = " . $value . "<br>";

$s = $s . "<p><h4>Session Values</h4>";
[COLOR=red]foreach($_SESSION as $key=>$value)[/color]
$s = $s . "<b>" . $key . "</b> = " . $value . "<br>";

$s = $s . "<p><h4>Cookie Values</h4>";
foreach($_COOKIE as $key=>$value)
$s = $s . "<b>" . $key . "</b> = " . $value . "<br>"; 

$s = $s . "<p><h4>GD Support</h4>";
foreach(gd_info() as $key=>$value)
$s = $s . "<b>" . $key . "</b> = " . $value . "<br>"; 

//$s = $s . "<p><h4>PHP Details</h4>";
//foreach(phpinfo() as $key=>$value)
//$s = $s . "<b>" . $key . "</b> = " . $value . "<br>"; 

echo $s;
?>
</body>
</html>

The actual error is "Invalid argument supplied for foreach()." Any ideas?
 
Sure. $_SESSION probably does not even exist if you do not do a session_start first.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
That's a possibility but, as the session was started by another page on the same site, I didn't think it needed to be started again. Does it? The same exact script never needed it before but you're right. Adding a link to the included session file did indeed fix the problem. Thanks!

Code:
<?php
include_once(getenv("DOCUMENT_ROOT") . "/includes/functions.inc");
?>

This script includes this function, among others:

Code:
<?php
function my_session_start() {
 	if (isset($_COOKIE['PHPSESSID'])) {
		$sessid = $_COOKIE['PHPSESSID'];
	} else if (isset($_GET['PHPSESSID'])) {
		$sessid = $_GET['PHPSESSID'];
	} else {
		session_start();
		return false;
	}
       
	if (!preg_match('/^[a-z0-9]{32}$/', $sessid)) {
		return false;
	}
	session_start();
 	return true;
}

my_session_start();
?>
 
You need to execute session_start(); in every page you need to have sessions in.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Interesting! I never had to do that before. It is my guess that my new setup does not have the Apache session auto-start setting enabled.
 
It is not an Apache setting but a PHP setting. But there is nothing wrong with issuing session_start for each page that needs it. It is how PHP works. If you want your code to be as independent as possible on the server settings, don't rely on automagic features like that.

Also a warning: session_start sends extra HTTP headers (to avoid caching). This is usually OK, but if you send "do-cache" headers (because IE needs them) do so AFTER the session_start or they will be overwritten without even a notice.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top