Passing Variable Using Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Passing Variable Using Session

Post by nitediver »

On the "first.php" page, the variable value(inside the session) deliver successfully, but on the "next.php" the session display nothing.
Its weird, since I also use session for login system and work without problem.

This is code from page before "first.php".

Code: Select all

<form name=\"update\" method=\"post\" action=\"z_reg_sess.php?prod_id=$prod_id\">

Code: Select all

<?
//first.php
include "function.php";
connect();

$_SESSION['prod_id'] = $_GET['prod_id'];

$_SESSION['size_id'] = htmlentities($_POST['size_id'], ENT_QUOTES);

echo "Product : ".$_SESSION['prod_id'];
echo "<br>";
echo "Size : ".$_SESSION['size_id'];
echo "<br>";
echo "<a href=\"z_next.php\">Next</a>";
?>
first.php Result:
--------------
Product : 12
Size : 1
--------------

Code: Select all

<?
//next.php
include "function.php";
connect();
session_start();

$_SESSION['prod_id'] = $prod_id = $_GET['prod_id'];
$_SESSION['size_id'] = $size_id = $_GET['size_id'];

echo "Product : ".$_SESSION['prod_id'];
echo "<br>";
echo "Size : ".$_SESSION['size_id'];
?>
next.php Result
-----------
Product :
Size :
-----------
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Passing Variable Using Session

Post by TheBrandon »

Are you passing the GET variable on next.php?

You're saying:

Code: Select all

$_SESSION['prod_id'] (which is 12) = $prod_id (which has no value yet) = $_GET['prod_id']
So if you don't have a get value set, you're setting the session to be nothing.

Try removing:

Code: Select all

$_SESSION['prod_id'] = $prod_id = $_GET['prod_id'];
$_SESSION['size_id'] = $size_id = $_GET['size_id'];
On next.php
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Passing Variable Using Session

Post by Jonah Bron »

And it doesn't look like you're calling session_start() on first.php.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Passing Variable Using Session

Post by nitediver »

@TheBrandon
Thanks, Ill try your advice.

@Jonah Bron
Yeah I forgot.
Post Reply