|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
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:
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
In that case, all you need is the following:
PHP Code:
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"]. |
|
#5
|
|||
|
|||
|
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? |
|
#6
|
|||
|
|||
|
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 ![]() |
|
#7
|
||||
|
||||
|
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:
FILE TWO PHP Code:
|
|
#8
|
|||
|
|||
|
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? |
|
#9
|
||||
|
||||
|
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.
|
|
#10
|
|||
|
|||
|
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.
|
|
#11
|
|||
|
|||
|
also how could i alter what you have given to create my own session_id? using say date, time, and ip address.
Thanks ceanth |
|
#12
|
||||
|
||||
|
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! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > how to check if session id has been created |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|