Page 1 of 1

Understanding SID

Posted: Wed Jun 28, 2006 6:43 am
by mparker1113
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I want to get an understanding of how SID can  be helpful to me. Is SID automatically generated when session_start() is declared? (that is what I thought would happen, but the below code does not show SID:

Code: Select all

<?php

session_start();

   session_register('count');
   $count = 1;
[color=red]
if(isset($SID))                                    //THIS CONDITION IS NOT BEING MET -- I THOUGHt THAT IT WOULD BE
echo "SID is $SID<br>";
[/color]

else echo "SID not set but count is: $count<br>";
?>
Thanks very much for any input on this.

- Mike


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]1.[/b] Select the correct board for your query. Take some time to read the guidelines in the sticky topic.[/quote]

Posted: Wed Jun 28, 2006 6:48 am
by JayBird
nope

Look at session_id()

FYI session_register() is old news

use this instead

Code: Select all

$_SESSION['varname']

Posted: Wed Jun 28, 2006 6:55 am
by mparker1113
I think that i had looked as session_id: This is what I am not understanding:

// session_id() is used to get or set the session id for the current session.

"The constant SID can also be used to retrieve the current name and session id as a string suitable for adding to URLs. " My question is: is the constant SID set automatically when session_start() is declared ( that is what I would have thought)

IF so, how come when I put in my code if(isset($SID)) following a session_start() declaration, the condition wasn't met ?? that is, $SID was not set even thought I had just started sessions.

Thanks for input superman -- you are lightning fast with response :D) .

Posted: Wed Jun 28, 2006 8:46 am
by JayBird
SID is a constant not a variable.

To check if a constant is set use defined()

Posted: Wed Jun 28, 2006 8:58 am
by mparker1113
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am having trouble for some reason. 

I have session_start() declared at the top of page 1.

Code: Select all

session_start();
if (! isset ($_SESSION['signed_in']))
   $_SESSION['signed_in']  = "no";
Then, if the user has not signed in

Code: Select all

( if( $_SESSION['signed_in'] == "no"))
I bring them to page 2, where I set the session variable

Code: Select all

$_SESSION['signed_in'] = "yes";           
     echo("<a href=\"index.php?".SID."\"> go back </a>");
and then send them back to page 1.

HOwever, for some reason, it keeps looping on page 1, not recognizing that I have set the variable, even though when I check on page 2, it shows that the session variable has been set

Code: Select all

echo("setting session variable it is: ". $_SESSION['signed_in']."<br>");
I don't know what the trouble is, I have done this before, but now am having trouble.


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jun 28, 2006 9:05 am
by JayBird
Show your complete code

And please use [ php ] tags

Posted: Wed Jun 28, 2006 9:06 am
by jamiel
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Well, first of all, I'm not sure why you are putting the session id in the URL. PHP tracks this automatically. How do you decide that a user is now "signed in" ? Presuming you have a function for that ...

Code: Select all

session_start();

if ('yes' == $_SESSION['signed_in']) {
        echo "You are signed in ..";
} else { header("Location: page2.php"); }
Page 2 needs to do whatever you need to do to sign them in, then just set $_SESSION['signed_in'] = 'yes';

Can't see why you need 'No'


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jun 28, 2006 9:14 am
by mparker1113
Admittedly the code is getting a little messy as I am progressing from what I expected to work to trying to get anything to work.

The way that I wanted to do this is :

1: set session_start on page 1
2: if (! isset($_SESSION['signed_in'] go to page 2
3: at page 2, set $_SESSION['signed_in'] and send back to page 1 where the session variable should now be recognized.

However, I noticed that when I set the variable on page 2, that it is not recognized on page 1. I shouldn't have to put session_start() on both pages, or anything else, should i ? Just putt session_start() on page 1, and I thought that I would be good to go.

Posted: Wed Jun 28, 2006 9:21 am
by JayBird
You need session_start() on ALL pages that use the session vars

Posted: Wed Jun 28, 2006 9:44 am
by mparker1113
Geesh -- that doesn't seem intuitive to me.

Session_use or something okay, but session_start ..

Thank you !

Very helpful !

Posted: Wed Jun 28, 2006 9:59 am
by technofreak
Session_start() is not like a key, which once open will be enough for the entire session. Session_start() is concerned with the global session array containing all your session variables.

Use of session_start() in a page, makes the code in the page aware of the session variables available as global variables to them. If you dont include it in a particular page, then its like the page is not told that there exists some global session variables that the page can make use of, for its operation.

session_start() in each page doesn't affect us session id or SID. SID is a constant value, which is set when your session starts. SID shouldn't be considered as a variable.

Posted: Wed Jun 28, 2006 10:17 am
by technofreak
When i was google'ing for something, i found this interesting tutorial about sessions. Beware it written when only PHP4 was out, sessions have undergone some improvement since the release of PHP5. One known change is you need not use session_register to register the session variables, rather just use the form..

Code: Select all

$_SESSION['variable_name'] = <value>;
You can refer http://www.zend.com/zend/tut/session.php if you are interested.. :) HTH

Posted: Wed Jun 28, 2006 10:23 am
by RobertGonzalez
Hey technofreak, you know Pimptastic already posted what you just posted in the last two posts? The stuff about session_register() and SID being constant. That was posted in the second and fourth posts of this thread. Wasn't sure if you saw that or not, just seems redundant to repost almost exactly what was already posted.

Posted: Wed Jun 28, 2006 10:27 am
by technofreak
Hi Everah, I indeed saw that. I just wanted to put a word about session id in simple 'lay man' terms. I am sorry that mine looked similar to that of pimptastic. I will try not to do the same mistake again :)

Posted: Wed Jun 28, 2006 11:12 am
by RobertGonzalez
No big. It just looks a little odd to some folks (like you are padding your post count, you know?). Not a big deal.