What's wrong with this session code?

Discussion in 'Programming' started by cipals15, Nov 29, 2008.

  1. #1
    It took me several times to run this code. But nothing seemed to work out. My server is Apache latest version and have php 5.x.x. It does not work out what i wanted.

    <?php
    session_start();
    if(!session_is_registered("count"))
    {
    	session_register("count");
    	session_register("start");
    	$count=0;
    	$start=time();
    }
    else
    {
    	$count++;
    }
    $sessionId=session_id();
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title>Sessions</title></head>
    <body>
    <p>This page points at a session (<?=$sessionId?>)<br/>
    count=<?=$count?>.<br/>
    start=<?=$start?>.
    <p>This session lasted
    <?php
    $duration=time()-$start;
    echo "$duration";
    ?> seconds.
    </body>
    </html>
    PHP:

     
    cipals15, Nov 29, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are local variables and session variables,
    the session variables are stored in the $_SESSION array on the server,
    the local variables you can use on the fly.

    when you use variables stored in the $_SESSION array,
    you copy the sessionvariable count to the local variable count

    $count=$_SESSION[count'];

    perform some operations on the local variable
    $count++;

    and store the local var count back to the sessionvariable
    $_SESSION['count']=$count;

    
    if(!isset($_SESSION['count']))
    {
        $count=0;
    	$_SESSION['count']=$count;
        $start=time();
    	$_SESSION['start']=$count;
    }
    else
    {
    	$count = $_SESSION['count'];
    	$count++;
    	$_SESSION['count'] = $count; 
    }
    $sessionId=session_id();
    
    PHP:
    (I use isset() in stead of session_is_registered.)

    In your code snippet
    
    else
    {
        $count++;
    }
    
    PHP:
    there is no copy of the $_SESSION array variable count to the
    local variable $count, that one starts fresh at 0 and gets 1 added
    so you always get 1 as value.
     
    juust, Nov 29, 2008 IP
  3. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #3
    Should i set

    session.save_path = "C:/WINDOWS/TEMP"

    in my php.ini?

    I am using Apache for windows. Is this the place where sessions are saved?
     
    cipals15, Nov 29, 2008 IP
  4. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    session.save_path = "c:/windows/temp"
    works fine,
    you can put any directory in php.ini as long as it is writable,
    the cookies are saved on the location you enter in php.ini

    you can use \\ when you want to access a win32 file system in php code
    c:\\windows\\temp\\
     
    juust, Nov 29, 2008 IP