Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2003, 09:03 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default arrays and sessions

I am trying to store an array in a session, and then print the contents of the array out on another page. however I cant get it to work, rather than printing the content of the array it jusy prints "Array".

The code I am using is

Code:

session_start();

$firstquarter=array('January', 'February', 'March');

$_SESSION["firstquarter"] = "$firstquarter";




Then on another page to write the contents of the array

Code:
print_r($_SESSION["firstquarter"]);


Or if I try this
Code:
foreach ($_SESSION["firstquarter"] as $value)
{
echo "$value<br>";
}

I get this error
"Warning: Invalid argument supplied for foreach() in C:\Xitami\webpages\results.php on line 97"
 
Old June 30th, 2003, 09:05 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry forgot to mention, I am using session_start() in the appropriate place on the second page.
 
Old June 30th, 2003, 12:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps print_r() or var_dump() will help you be less dazed and confused about what your variable names, types, and values are...

http://www.php.net/print_r
http://www.php.net/var_dump


Take care,

Nik
http://www.bigaction.org/
 
Old June 30th, 2003, 12:27 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

that SHOULD work, i have tried an equivilant version, you can test to make sure your array exists by making an if statement before your foreach:
if($_SESSION["firstquarter"]) echo "this works";

----------------------------
http://aeonofdarkness.com
 
Old June 30th, 2003, 01:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by natmaster
 that SHOULD work, i have tried an equivilant version, you can test to make sure your array exists by making an if statement before your foreach:
if($_SESSION["firstquarter"]) echo "this works";
Personally, I find this style to be horrible. Don't take it personally, it's just the way you learned PHP.

The reason I think this is a Bad Thing (tm) is that if $_SESSION["firstquarter"] doesn't exist, PHP will issue an E_NOTICE level warning that you're accessing an uninitialized index, and create the index variable with a default value of false, "", or 0, depending on context.

You should always be explicit when checking for the existence or type of a variable to avoid ambiguous code and potential security risks.

Code:
if( isset($_SESSION['firstquarter'])    &&
    is_array($_SESSION['firstquarter'])   )
{
// do your stuff here
}
Often times, it's best to use negative logic to avoid having the bulk of your code in an if() block. Use the if() statement to determine if there's an error (i.e. firstquarter doesn't exist) and deal with it appropriately:

Code:
if ( !isset($_SESSION['firstquarter']     ||
     !is_array($_SESSION['firstquarter'])   )
{
    // exit w/ error?
    // redirect?
    // initialize $_SESSION['firstquarter']?
    // up to you.
}
// Being here in the execution implies that
// $_SESSION['firstquarter'] exists AND it is an array.
// do your stuff here.


Take care,

Nik
http://www.bigaction.org/
 
Old June 30th, 2003, 10:54 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

i didn't know there was a difference.... i just started learning php and have a background with C/C++ so i'm used to doing stuff like that. :) unfortuneatly i didn't really understand what you were saying about it...could u explain that a bit more? i like to fix bad habbits before they become habbits.

----------------------------
http://aeonofdarkness.com
 
Old July 1st, 2003, 04:22 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers guys I got it working now. The strange thing is the first part of the code which set the array to the session was on a page which redirected to the page where the array was called, and that wasnt working. Then when I put the code that set the array to the session at the top of the page where the array was called, it worked. I dont understand the difference?
 
Old July 1st, 2003, 12:23 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

if the line of code that redirected is before you set the array, then it will redirect before it sets the array.

----------------------------
http://aeonofdarkness.com
 
Old July 2nd, 2003, 02:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by natmaster
 i didn't know there was a difference.... i just started learning php and have a background with C/C++ so i'm used to doing stuff like that. :) unfortuneatly i didn't really understand what you were saying about it...could u explain that a bit more? i like to fix bad habbits before they become habbits.
Definitely. If you try to use a non-existent variable, PHP will CREATE that variable for you, automatically. PHP will also issue a NOTICE level warning (the lowest severity level), which many people have disabled by default.

For example, take this simple script:

Code:
<?php
echo "Hello, " . $name;
?>
Obviously, $name doesn't exist.

Check your error_reporting setting in php.ini. Set it to E_ALL. Run the script. You should get a NOTICE level warning saying that you're using an uninitialized variable.

Change error_reporting to E_ALL & ~E_NOTICE. That's "Everything except E_NOTICE", by the way.

Run the script again. No errors.

In both cases, PHP creates the variable $name and gives it a default value. Since you're using the variable in string context, the variable is treated as an empty string. If you were using it in an integer context, it would be 0. If in a boolean context, it would be false.

The problem with your code is that it won't work on ALL installations of PHP. What happens when you upload your script to an ISP that has error_reporting set to E_ALL? Your pages get displayed with errors, that's what.

Is the existence of that variable really proof enough that it's what you want it to be? No. You're expecting an array. If that variable is set to any other data type, you'll get errors in the foreach() loop.

If register_globals is on, what happens when a malicious user requests your page and tacks on "?_SESSION[firstquarter]=0" to the URL?

See what I'm getting at?


The best way to write your scripts is to set PHP to it's most restrictive configuration -- error_reporting = E_ALL, register_globals = OFF -- and go from there.

Once your site runs cleanly here, it should run cleanly anywhere else, and you've probably made your site much more secure against malicious users trying to hack your site by submitting invalid GET, POST, and COOKIE data.


For more info, I strongly suggest you read my register_globals FAQ from the old lists, and the Security section of the PHP manual online:

  http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
  http://www.php.net/security
  http://www.php.net/security.errors
  http://www.php.net/security.registerglobals




Check your error_reporting setting in php.ini.

Take care,

Nik
http://www.bigaction.org/
 
Old July 3rd, 2003, 01:24 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

it creates the variable......that would make sense because of it's dynamic look at variable creation, thanks for the explanation. :)

----------------------------
http://aeonofdarkness.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays and sessions dazednconfused PHP How-To 2 April 27th, 2007 12:16 PM
sessions p2ptolu Classic ASP Components 3 March 17th, 2005 06:31 AM
Multidemmesional Arrays OR arrays gmoney060 Classic ASP Basics 3 November 1st, 2004 03:42 PM
Mixing classic ASP sessions with ASP.NET sessions scorpion_king General .NET 2 August 4th, 2004 08:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.