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

isset() Problem

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
0
0
KW
Hi there,

I have a page that have the following code:

<?
if (!isset($SID))
{
$SID = mt_rand();
echo $SID;
}
else
{
echo &quot;SID already set&quot;;
}
?>

The problem is everytime I hit refresh button (F5) the variable is re-created! I want only to create that variable only if its the first time the user enter that page.

Please I appreciate your help.
 
Use sessions...

<?
session_start();

if (!isset($_SESSION['sid'])) {
$_SESSION['sid'] = mt_rand();
echo $_SESSION['sid'];
} else {
echo &quot;sid already set&quot;;
}
?>


-Rob
 
Depending on what you want to use the [tt]$SID[/tt] variable for, you could just use the session ID instead (I'm guessing this is what you want since your variable is named [tt]$Session ID[/tt]). [tt]session_id()[/tt] gives you the ID. //Daniel
 
Forget that SID is SessionID, assume it any varibale (lets say ID) and I want this variable (ID) to be careated once, and when the user hit the refresh button (F5) it will not be re-created.

So the code will be:

<?
if (!isset($ID))
{
$ID = mt_rand();
echo $ID;
}
else
{
echo &quot;ID already set&quot;;
}
?>

One more thing I dont want to use session_start() becuse this command requires a cookie, and maybe the user disabled the cookies on his/her PC.

Please I apreciate you help.
 
sessions will still work if the user has disabled cookies.

You will see that urls will get rewritten to be of the form yourpage.php?sid=6762567498

which is how php keeps track of the session between pages, the session info is stored on the web server (to see where check out your php.ini)
 
I can't remember which PHP's version that my webHost is using, but what I can remember very good that the PHP's version that my webHost is using is if the cookie is disabled the variable will not be inserted in the URL automatically! from this point I dont want to use sessions.

So, is there is any way to stop re-creating (ID) variable everytime the user hit the refresh button?

I know that in PHP you can play around anything and implement your crazy idea:)
 
So put it in the URL yourself...

<?php

if (!isset($_GET[&quot;sid&quot;])) {
$id = mt_rand();
header(&quot;Location: sid=$id&quot;);
} else {
echo &quot;Already set to &quot;.$_GET[&quot;sid&quot;];
}
?>


-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top