General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 8th, 2003, 11:31 AM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how to check if session id has been created

I want to check if the session id has been created otherwise create one. I used this code but it doesnt seem to work:

<?
if(isset(session_id))
{
echo "session has started";
}
else {
<?
$time = time();
$date = $today = date("Ymd");
$id = $time + $date;

session_id($id);
session_start();
session_register(session_id);

print session_id();
echo "time = $time";
echo "date = $date";

?>

I placed this at the top of the page with some html coding under it but the page appears blank for some reason, what is another way of doing this or am i doing something wrong?

Reply With Quote
  #2  
Old July 8th, 2003, 01:29 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Looks like you've left opening and closing parentheses off your call to session_id() in the if statement. All you should need to do is the following:

PHP Code:
if(session_id()){
    echo 
"session has started.";
}
else{
    
session_start();
    print 
session_id();



You shouldn't need to pass date or time or anything to your session, and you won't need to register session_id as a session variable. The ID will always be available through session_id() if a session has been started.

Reply With Quote
  #3  
Old July 8th, 2003, 02:05 PM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ok i have this:

<?

if(session_id()){
echo "session has started.";
}
else{
session_start();
$time = time();
$date = $today = date("Ymd");
$id = $time + $date;

session_id($id);

print session_id();
echo "time = $time";
echo "date = $date";
}
?>

But if i hit the refresh button on my browser it just displays the updated session_id. I want it so that once the session id has been made it wont make anymore.

Reply With Quote
  #4  
Old July 8th, 2003, 04:22 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
In that case, all you need is the following:

PHP Code:
 session_start();
echo 
session_id(); 


If no session exists, one will be created. If one exists, it's just carried over to this page. I created a file with that code in it. On the first load, it created a session id. On refresh, it kept the same session id. If you want $time and $date to carry over to subsequent pages, you'll need to put them in the session scope by issuing the commands "session_register('time')" and "session_register('date')" -- but when you use these variables on this and future pages, you'll still access them as $time and $date or, if register_globals is turned off, as $_SESSION["time"] and $_SESSION["date"].

Reply With Quote
  #5  
Old July 8th, 2003, 04:26 PM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
interesting, what if i want to call up the session_id again, would i have to use session_register(session_id); ?

How would i display this id, if i was to use

session_start();
echo session_id();

could that not create a new session_id?

Reply With Quote
  #6  
Old July 8th, 2003, 04:29 PM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
page1:

if(session_id()){

echo "session has started.";

}

else{

session_start();
echo session_id();

}


page2:
<?
session_start();
echo session_id();

?>

There is no output on page2

Reply With Quote
  #7  
Old July 8th, 2003, 04:38 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Create a file that contains nothing but my last sample block and a link to the second page. For the second page, duplicate the first page, but make the link go back to the first page.

I did this and had no problems. If a session_id exists, session_start() knows to look for session variables that the existing session_id (stored in a session cookie that expires when the browser is closed) points to. Else, it creates a session_id, which is stored in a session cookie and is available for use later.

Scrap all the if statements and have two files, as follows:

FILE ONE
PHP Code:
 session_start();
echo 
session_id();
print 
"<a href=\"file2.php\">File 2</a>"


FILE TWO
PHP Code:
 session_start();
echo 
session_id();
print 
"<a href=\"file1.php\">File 1</a>"

Reply With Quote
  #8  
Old July 8th, 2003, 04:49 PM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it works! But there is something strange. I had the same as what you had and it works but i had this html coding at the bottom of file1:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<a href="http://...................../sessionid2.php">page2</a>
</body>


</html>

So the output of file1 is the session_id then 2 links, one to file 2 from what you had (which is in the <? ?>) and this one which was in the html tags. When i clicked ur link it works fine, but when i click the html link it doesnt work.

I then changed the http://............ to just the php file and it worked(as you had done it). I was wondering why it was not working with the http://......... and works with only the direct .php reference?

Reply With Quote
  #9  
Old July 8th, 2003, 04:57 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
You've got me. Are you sure you had your link typed in correctly? A typo's the only thing I can think of that'd cause the problem you're now experiencing.

Reply With Quote
  #10  
Old July 9th, 2003, 05:14 AM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i was thinking, is there any way in which i can assign the session_id to a variable. This way i could register the variable using session_register then make it easier when i want to use this session_id (now a variable) to add to a table.

Reply With Quote
  #11  
Old July 9th, 2003, 05:51 AM
ceanth ceanth is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: London
Posts: 7 ceanth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
also how could i alter what you have given to create my own session_id? using say date, time, and ip address.

Thanks

ceanth

Reply With Quote
  #12  
Old July 9th, 2003, 08:05 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I'm not sure you gain anything by assigning the session_id to a variable. Anywhere you can use a variable, you can just do a call to session_id() and save yourself that little bit of memory. Unless you're going to be using the value a number of separate times, I see no point in adding that little bit of overhead.

As for creating your own session_id, I'm not sure you can do that. I suspect you can't, as I believe they're usually generated using a random seed and probably the system time from the Web server (though I'm not sure about that) run through an md5 hash. If you figure out how to generate a session_id or how to deconstruct one, I'd love to have that info handy, so please post it!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > how to check if session id has been created


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT