Page 1 of 1
session help
Posted: Thu Mar 06, 2003 11:23 am
by aboo_real
First of all, I read the session thing on the top of this forum, I copied and paste the code and got a
Notice: Undefined index: username in c:\apache group\apache\htdocs\chapter20\page2.php on line 5
$_SESSION[username] equals
I have researched a lot of places but I still can't get my sesison to work. I changed to the global thing on and off still no luck. I used the session_register() methods and the $_SESSION[] methods, still non of them works. I checked the path and the session files are all there. I either get the undefined index error or undefined variable error. So, would anyone help?

I am running php 4.30 with apache 1.3.27 on a win xp pro. I got the Sams "php and mysql web development" book as a reference. Would someone please help me here.(Haven't slept for 2 days just trying to figure out what is going on) THNX~
Posted: Thu Mar 06, 2003 11:44 am
by pootergeist
try the same with quotes around the associative index
$_SESSION['quoted_index'] <--- spot the quotes

Posted: Thu Mar 06, 2003 12:39 pm
by rodrigocaldeira
Try this to:
The $_SESSION['variable'] is equals to $variable. But, you must to register the $variable with session_register(), like this:
session_register($variable);
$variable = <something>
.....
In each file of your system or website, use the session_register() to register your sessions!!!!! This is a secure mode....
Posted: Thu Mar 06, 2003 2:15 pm
by daven
If you are using the superglobal $_SESSION, do not use session_register. To create a session variable, just do this:
$_SESSION['my_var']=$value;
Posted: Thu Mar 06, 2003 6:11 pm
by aboo_real
First of all, I really appriciate you guys' help. However, my problem persists. The following is my page1.php:
<?php
session_start();
if ( !isset($_SESSION['username']) ) {
$_SESSION['username'] = 'jason';
}
session_register("ses");
$ses = "fun";
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo "<br>\$ses is $ses";
echo '<br><br><a href="page2.php">Go to page 2</a>';
?>
then my page2.php
<?php
session_start();
echo "\$ses is $ses<br>";
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo '<br><br><a href="page3.php">Go to page 3</a>';
?>
and this is what I got when I get to page 2.
Notice: Undefined variable: ses in c:\apache group\apache\htdocs\chapter20\page2.php on line 5
$ses is
Notice: Undefined index: username in c:\apache group\apache\htdocs\chapter20\page2.php on line 6
$_SESSION[username] equals
Go to page 3
Please, can anyone help me with this one?
Posted: Fri Mar 07, 2003 2:00 am
by twigletmac
Do not use
session_register() alongside using $_SESSION as it won't work, try changing your code to:
Code: Select all
<?php
/* PAGE 1 */
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['username'] = 'jason';
}
$_SESSION['ses'] = 'fun';
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo '<br>$_SESSION[ses] is '.$_SESSION['ses'];
echo '<br><br><a href="page2.php">Go to page 2</a>';
?>
<?php
/* PAGE 2 */
session_start();
echo '$_SESSION[ses] is '.$_SESSION['ses'].'<br>';
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo '<br><br><a href="page3.php">Go to page 3</a>';
?>
Mac
Posted: Fri Mar 07, 2003 2:03 am
by twigletmac
rodrigocaldeira wrote:In each file of your system or website, use the session_register() to register your sessions!!!!! This is a secure mode....
This is incorrect - if you are using PHP 4.1 or above then you must not use session_register(), instead just use the $_SESSION array.
For more information please read:
http://www.php.net/manual/en/function.s ... gister.php
especially the bit where it says:
php manual wrote:Caution
If you want your script to work regardless of register_globals, you need to use the $_SESSION array. All $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where register_globals is disabled.
AND
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered() and session_unregister().
Mac
Posted: Fri Mar 07, 2003 2:22 am
by aboo_real
Thank you for your time, but I have learned to use $_SESSION[] as well, still my problem does not go away. I had an interesting discovery though. When I access the page1.php, a session file containing the session variable is created. When I access the page2.php from page 1, an empty session file is created. Is this normal??
Posted: Fri Mar 07, 2003 2:23 am
by twigletmac
What does your code look like now?
Mac
Posted: Fri Mar 07, 2003 2:45 am
by aboo_real
page1.php
<?php
session_start();
if ( !isset($_SESSION['username']) ) {
$_SESSION['username'] = 'jason';
}
$_SESSION['ses'] = "fun";
$ses = "fun";
echo '$_SESSION[username] equals '.$_SESSION['username'];
echo "<br>\$ses is ".$_SESSION['ses'];
echo '<br><br><a href="page2.php">Go to page 2</a>';
?>
page2.php
<?php
session_start();
if(!isset($_SESSION['username'])){
echo "session not set<br>";
}
if(!isset($_SESSION['ses'])){
echo "\$ses not set<br>";
}
// If session vars are set, display vars
if(isset($_SESSION['username'])){
echo "The session vars are set <br>";
echo "\$ses = ".$_SESSION['ses']."<br>";
echo "Username is ".$_SESSION['username']."<br>";
}
?>
Posted: Sun Mar 16, 2003 4:07 pm
by aboo_real
FINALLY WORKING!!!
I re-intsalled apache 1.3.27 with source and the latest PHP 4.3.2rc
and I see my FIRST SESSION variable appear on the next page!!!!
THANK YOU..I AM SO HAPPY~
