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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to store arrays into sessions? 2

Status
Not open for further replies.

Joulius

Programmer
Dec 16, 2003
215
RO
Hi there!
I'm using Apache/1.3.14 (Win32) + PHP Version 4.3.2-RC1 locally.
I have a page that reads 2 files from 2 folders and stores them into arrays. Now, I want to pass that sessions to another page and display the arrays.
Here's the code from first page:
Code:
<?
session_start();
$emailDir="./email/";
$messageDir="./message/";
$dire=opendir($emailDir);
$dirm=opendir($messageDir);
while (false !== ($fe = readdir($dire))){
	if(is_file($emailDir.$fe)){
		$fecontents = file($emailDir.$fe);
		$i=0;
		while (list($line_numE, $lineE) = each ($fecontents)) {
			if(ereg("([_a-zA-Z0-9-]+)@([a-zA-Z0-9-]+)\.([a-zA-Z0-9-]{2,})",$lineE)){
		    	$email[$i]=$lineE;
				$i++;
			}
		}
		unlink($emailDir.$fe);
	}
}
while (false !== ($fm = readdir($dirm))){
	if(is_file($messageDir.$fm)){
		$fmcontents = file($messageDir.$fm);
		$message=$fmcontents;
		unlink($messageDir.$fm);
	}
}
session_register("EMAIL");
session_register("MESSAGE");
$eml=array_unique($email);
$EMAIL=$eml;
$MESSAGE=$message;
?>
And here's the second page:
Code:
<?
session_start();
if((!session_is_registered("EMAIL"))or(!session_is_registered("MESSAGE"))){
	header("Location: start.php");
	exit;
}
$eml=$EMAIL;
$cntEml=count($eml);
$msg=$MESSAGE;
$cntMsg=count($msg);
echo "Email list:<br>";
for($i=0;$i<$cntEml;$i++){
 echo $i." ".$eml[$i]."<br>";
}
echo "The message:<br>";
for($j=0;$j<$cntMsg;$j++){
 echo $msg[$j];
}
?>
Locally, it works great, but on web hosting doesn't work at all. It displays both sessions as empty.
The strange thing is that <? phpinfo() ?> doesn't seem to work on that hosting, so I don't know what version is their PHP, so, if anyone have any ideea, it's my code wrong?
The PHP version from that hosting could be the problem?

[morning]
 
Session_register should create the $MESSAGE and $EMAIL variables.

Also i see here a programming error due to the diferences of the windows php and linux php config files.

Using $email on the line
$eml=array_unique($email); will give empty value cuz the $email variable it's defined local in the while code block

what i mean it's this.

$i=0;
{
$j=100;
}
$i=$j;

If the php doesnt treat whole variables as globals $i wont be 100.

Bad english here but i think i've made my point.


________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
shaddow:

If you use session_register() on variable names that don't exist, yes, PHP will create them in the session. However, those variables will be empty.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes, it's empty cuz the $email and $message variable are local to while code block.
it should be something like

Code:
$email=array();
$messages=array();
...
while()
{
}
...
$EMAIL=$email;

Also if the register_globals is off then you can use
Code:
foreach($_POST as $key=>value)
  $$key=$value;
this gives you ability to access $varname instead of $_POST["varname"]


________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
minor mistake here
foreach($_POST as $key=>value)
should be
foreach($_POST as $key=>$value)

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
He uses $email as intermediar variable as i see from it's code and therefor to use it outside it's definition block it needs to define that in same block as the $EMAIL=$email which is exactly what i've sayed with this code
Code:
$email=array();
$messages=array();
...
while()
{
}
...
$EMAIL=$email;

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
shaddow:
Wait a minute.

While loop blocks do not have local variables in PHP. Only functions have local variables. Read here for more information.


Joulius:
There are known issues with using session_register(). You can have problems when register_globals is set to "off". I strongly recommend that instead of using session_register(), you instead instantiate elements inside the $_SESSION array directly.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes i see now what you ment, but i had some problems using variables in diferent code blocks. That's why i initialize my variables before just to avoid that.
Anyway using $_SESSION could not work on php < 4.1 as there is the older version $HTTP_SESSION_VARS...

Anyway my opinion is that better should make an simple php with <?echo phpinfo()?> and see if it has register_globals on and what version of php it has.
I've seen free PHP hosting with php < 4.1 and Mysql 3.35 or older.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
shaddow:
It is always better to simply write your code with the assumption that register_globals will always be off. It causes less heartache.


Joulius:
I strongly recommend that you get away from using session_register(), session_is_registered(), and references to session variables in global contexts. If register_globals is set to "off", you're going to have problems.

In the first script, I recommend you replace:

Code:
session_register("EMAIL");
session_register("MESSAGE");
$eml=array_unique($email);
$EMAIL=$eml;
$MESSAGE=$message;

with
Code:
$_SESSION['EMAIL'] = array_unique($email);
$_SESSION['MESSAGE'] = $message;


In the second script, I recommend that you make the replacements I've shown in red:

Code:
<?
session_start();
if([red]!isset($_SESSION["EMAIL"])or !isset($_SESSION["MESSAGE"])[/red])
{
    header("Location: start.php");
    exit;
}
$eml=[red]$_SESSION['EMAIL'][/red];
$cntEml=count($eml);
$msg=[red]$_SESSION['MESSAGE'][/red];

If your version of PHP is older than 4.1.0, replace "$_SESSION" in my above recommendations with "$HTTP_SESSION_VARS"

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thank you both!
It seems that PHP causes me more headaches than ASP.NET :S.

[morning]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top