SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session me aarghh

    ahh well, here we go,
    I have a problem understanding sessions, I don't know how to it? I read the tutorial by Kevin Y. explaining how to do a User Managment system but not quite fully sure I know it. So can someone please write an example on how to use sessions (two pages)

    Thank you guys!

    while(true) sheep++;

  2. #2
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sessions is simple. The Basic rules are :

    on every page that you want to use sessions on, the first line should have :
    session_start();

    now, suppose you want to store 2 variables in the session, var1 and var2. You simply have to do :
    session_register(var1);
    session_register(var2);


    to use these variables in your program, simply refer to them as :
    echo $var1;
    echo $var2;

    if you want to delete the variables, do this :
    session_unregister(var1);
    session_unregister(var2);


    if you want to destroy the session itself, do this :
    session_destroy();


    thats about all there is
    Kunal
    i dunno...

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm..Just to clarify..so if you want to assign different variable name for your sessions variables..you do this?

    $session["var1"] = $value1;
    $session["var2"] = $value2;

    else it'll be $var1 and $var2 right?
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh, thank you!
    It works, I know it (hehehe)!

    P.S> does anyone know how to set a cookie and read it ??
    thank you!
    while(true) sheep++;

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To set a cookie,

    PHP Code:
    if ($HTTP_HOST != $SERVER_NAME) { // If no 'WWW', Cookie won't work in Netscape
        
    header ("Location: http://www.$HTTP_HOST$SCRIPT_NAME?$QUERY_STRING");
        exit;
    }
    setcookie ("cookiename"cookievariabletime()+31536000"/"".$HTTP_HOST"0); // Sets a Cookie which will Expire in a Year's time 

    to read a cookie, just read it by accessing the cookiename variable.

    like..

    PHP Code:
    // To check if cookie exists

    if ($cookiename) { echo 'Cookie Exists'; }
    else { echo 
    'Cookie does not Exist'; } 
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  6. #6
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get it? The first part of the code? Why all of that relocating and doing that and that ....

    can you do it a function sort of way? You know like
    a function that calls let say SetC("myname", "value of cookie?"); or something like that?

    ??
    while(true) sheep++;

  7. #7
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yes.you can create a function..

    PHP Code:
    function setCookie($cookiename$cookievar$time)
    {

        if (
    $HTTP_HOST != $SERVER_NAME) { // If no 'WWW', Cookie won't work in Netscape
            
    header ("Location: http://www.$HTTP_HOST$SCRIPT_NAME?$QUERY_STRING");
            exit;
        }
        
    setcookie ("$cookiename"$cookievar$time"/"".$HTTP_HOST"0);


    the purpose of redirecting to ensure a www in the host name..coz cookies won't work in netscape without the 'www'. its for compatibility reasons.
    Last edited by lynlimz; Aug 12, 2001 at 07:48.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  8. #8
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but still:

    Okay, we got the function, what about the check if cookie exist? How do you do something if(function($cookiename)) { do smothenig etc.??
    Can you have a function that is also a variable?

    You know that if you have a function checkcookie that it can be true or false and you can check it by running it?
    while(true) sheep++;

  9. #9
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It checks if cookie is set. else create the cookie.
    if cookie exists, return false.
    if cookie doesn't exist, return true.

    PHP Code:
    function chkCookie($cookiename$cookievar$time)
    {

        if (!$
    $cookiename) {
            if (
    $HTTP_HOST != $SERVER_NAME) { // If no 'WWW', Cookie won't work in Netscape
                
    header ("Location: http://www.$HTTP_HOST$SCRIPT_NAME?$QUERY_STRING");
                exit;
        }
        
    setcookie ("$cookiename"$cookievar$time"/"".$HTTP_HOST"0);
        return 
    false;
        }
        else {
            
    // Do stuffs when cookie exists. or like now, return true
        
    return true;
        }


    Last edited by lynlimz; Aug 12, 2001 at 07:50.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •