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

Cookies and PHP ?

Status
Not open for further replies.

maverick

MIS
Apr 21, 1999
193
US
I'm having a problem with cookies and php
"This is out of a book"

my first page is:
<?
session_start();
$sess_var = "Hello world!";
session_register("sess_var");
echo "The content of \$sess_var is $sess_var<br>";
echo "<br>" ;
print_r ($_SESSION);
echo "<br>" ;
print_r ($_COOKIE);
echo "<br>" ;
?>
<br>
<a href = "page2.php">Next page</a>

my page2 is:
<?
session_start();
echo "<br>" ;
print_r ($_SESSION);
echo "<br>" ;
print_r ($_COOKIE);
echo "<br>" ;
echo "The content of \$sess_var is $sess_var" ;
echo "<br>" ;
//session_unregister("sess_var");
?>
<a href = "page3.php">Next page</a>

the problem is that it will not carry the value to the next page ??????

I think it's a setup problem, because it does not create a cookie on the browsing machine...

tia


&quot;Hacker by Heart&quot;
saenzcorp@hotpop.com
 
I'd bet the value is being carried over to the next page.

The problem is that PHP has a configuration setting called "register_globals". When this is set to "on", variables in $_GET, $_POST, and $_SESSION are instantiated as singleton variables. For example, $_POST['foo'] is created also as $foo.

However, for security reasons, the default setting since PHP 4.1.2 has been "off". (See FAQ434-2999 for more information.) This causes problems with using session_register(). The PHP online manual implies that instead of using session_register(), you simply set elements of the $_SESSION array directly.

Your first page would read something like:
Code:
<?
session_start();
$sess_var = "Hello world!";
$_SESSION["sess_var"] = $sess_var;
echo "The content of \$sess_var is $sess_var<br>";
echo "<br>" ;
print_r ($_SESSION);
echo "<br>" ;
print_r ($_COOKIE);
echo "<br>" ;
?>
<br>
<a href = "page2.php">Next page</a>

Your second script would read:
Code:
<?
session_start();
echo "<br>" ;
print_r ($_SESSION);
echo "<br>" ;
print_r ($_COOKIE);
echo "<br>" ;
echo "The content of \$sess_var is ' . $_SESSION['sess_var'];
echo "<br>" ;
//session_unregister("sess_var");
?>
<a href = "page3.php">Next page</a>

That is probably why $sess_var doesn't have a value. Does the print_r() of $_SESSION output anything? If it does not, what version of PHP are you running? The superglobal arrays ($_POST, $_GET, $_COOKIE, $_SESSION, etc.) do not exist in the language until PHP version 4.1.0. Prior to that, you should use $HTTP_GET_VARS, $HTTP_POST_VARS, etc.).

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I just wanted to thank you for the prompt response. I took a look at the code you suggested and the good news is that your suggestion got me to my desired results. I needed to make a slight change in the code as follows:

echo "The content of \$sess_var is " . $_SESSION['sess_var'] ;

and it worked perfectly !
Again, you ROCK! Thanks for the help!

Mav

&quot;Hacker by Heart&quot;
saenzcorp@hotpop.com
 
I just realized this was not a cookie issue at all...

this is a session issue...

I'll get this ;-P

&quot;Hacker by Heart&quot;
saenzcorp@hotpop.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top