Forum Moderators: coopster

Message Too Old, No Replies

Sample code to pass variable using PHP session...

         

irock

5:40 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi I would like to know if there's some sample PHP for passing a variable (a mile long URL in my case) to another page via simple PHP session. Will there be any circumstances in which a user environment can affect the stability of PHP session?

Thanks for any info.

sabai

5:50 am on Jul 27, 2003 (gmt 0)

10+ Year Member



That's what sesions are for... if you have started a PHP session then just do this -

$_SESSION['url']='http://milelongurl';

Then you can access it on pages thereafter as

$_SESSION['url']

irock

6:25 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



THanks! Just a few more questions.

When will session end by itself? Is it recommended that the server should flush the PHP sessions periodically?

Most importantly, I saw a lot of codes involving PHP session start... but none of them has a simple sample code. DO you know any?

Thanks again!

jaski

6:48 am on Jul 27, 2003 (gmt 0)

10+ Year Member



Yes session will end by itself.

Default length of session is 15 minutes from the last activity.

Sample code
<?
$myvar = "abcd";
session_register('myvar');
?>

Things to note

1) its 'myvar' and not '$myvar' inside the brackets.
2) $myvar should be a global variable...else it will not register.

irock

6:58 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did something like but won't work. Do you know why?

<?
global $merchanturl, $merchanturl2, $merchanturl3, $merchanturl4;
session_start();

$merchanturl = "xxx";
$merchanturl2 = "yyy";
$merchanturl3 = "zzz";
$merchanturl4 = "aaa";

session_register("merchanturl");
session_register("merchanturl2");
session_register("merchanturl3");
session_register("merchanturl4");

print "<a href=http://www.domain.com/prices/redirect.php>COME HERE!</a>";
?>

THis is the redirect.php file...

<html>
<head><title>Sessions</title></head>
<body>
<br>merchant url 1 = <? print $_SESSION['merchanturl'];?>..
<br>merchant url 2 = <? print $_SESSION['merchanturl2'];?>..
<br>merchant url 3 = <? print $_SESSION['merchanturl3'];?>..
<br>merchant url 4 = <? print $_SESSION['merchanturl4'];?>..
</body>
</html>

[edited by: irock at 7:21 am (utc) on July 27, 2003]

sabai

7:07 am on Jul 27, 2003 (gmt 0)

10+ Year Member



The url in the anchor has to be relative... PHP does this so that it doesn't pass the session ID to external sites... instead of

<a href=http://www.domain.com/prices/redirect.php>COME HERE!</a>

use

<a href=/prices/redirect.php>COME HERE!</a>

have fun

[edited by: sabai at 7:36 am (utc) on July 27, 2003]

irock

7:21 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... I tried the relative path thing, but session variables still won't show up. Any ideas?

Also, when I tried to destory the session, I get this...
Warning: Trying to destroy uninitialized session in prices/host.php on line 7

Warning: Cannot send session cache limiter - headers already sent (output started at host.php:7) in prices/host.php on line 15

sabai

7:28 am on Jul 27, 2003 (gmt 0)

10+ Year Member



If register_globals is disabled then you will have to set the variable with
$_SESSION['variablename']='a'
and not
session_register('variablename')='a'

Check all of your session settings with

phpinfo()
and see if anything looks wrong. Looks like the session isn't being started.

good luck

irock

7:32 am on Jul 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have session.auto_start (OFF) in my phpinfo();

BTW, sabai, could you check your private message? THanks!

sabai

7:40 am on Jul 27, 2003 (gmt 0)

10+ Year Member



If you are using session_start then you don't need auto_start...

Maybe a chached version of the redirect.php page is being loaded - can you flush your cache and see if it helps