SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beginner problem: Session doesn't work!

    Hi,

    I'm used to coding ASP pages, but have been asked to do some php - about which I know nothing.

    I've found the following code which is supposed to set a session variable:

    Code:
    <?
    session_start();
    session_register("SESSION");
    
    if (! isset($SESSION)) {
    	$SESSION["count"] = 0;
    	echo "<li>Counter initialized, please reload this page to see it increment";
    } else {
    	echo "<li>Waking up session $PHPSESSID";
    	$SESSION["count"]++;
    }
    echo "<li>The counter is now $SESSION[count] ";
    ?>
    However, this doesn't work. It simply keeps displaying '0' as the value, and doesn't increment as it's supposed to.

    I thought this might be because PHP isn't available on the server or something (since I've tried setting a cookie which also doesn't work) but I can run a script which sends email by php and that works fine?!?

    Can anyone shed any light on this??

    Thanks very much...

  2. #2
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo "<li>The counter is now $SESSION[count] "
    You need to make the array key (count) a string: 'count'

    PHP Code:
    echo "<li>The counter is now $SESSION['count'] "
    Ideally, though, I would avoid session_register and use the $_SESSION array:

    PHP Code:
    <?php
    session_start
    ();

    if (! isset(
    $_SESSION['count'])) {
        
    $_SESSION['count'] = 0;
        echo 
    '<li>Counter initialized, please reload this page to see it increment';
    } else {
        echo 
    '<li>Waking up session '.session_id();
        
    $_SESSION['count']++;
    }
    echo 
    '<li>The counter is now '.$SESSION['count'];
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Neither of the above seem to work unfortunately. The first gives the following error:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in e:\domains\m\mydomain.co.uk\user\htdocs\testdirectory\sessiontest3.php on line 12



    The second solution says:

    Waking up session ee3473022bb9aeaafdc1b1bcc4ac0833
    The counter is now


    It doesn't show a counter value at all.

    Iny ideas?

  4. #4
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just as an aside, the W3C's simple cookie scripts don't work either:

    PAGE1 (sets cookie)
    Code:
    <?php 
    setcookie("uname", $name, time()+36000);
    ?>
    <html>
    <body><p>
    A cookie was set on this page! The cookie will be active when 
    the client has sent the cookie back to the server.
    </p>
    <a href="cookietest2.php">SEE...</a>
    </body>
    </html>
    PAGE2 (retrieves cookie)
    Code:
    <html>
    <body><?php
    if (isset($_COOKIE["uname"]))
    echo "Welcome " . $_COOKIE["uname"] . "!<br />";
    else
    echo "You are not logged in!<br />";
    ?></body>
    </html>
    This just tells me "You are not logged in!"

    I know the difference between sessions and cookies, and would prefer to use session variables. However, I thought the fact that neither of these simple trials works, might highlight the problem to someone who knows PHP.

  5. #5
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just tried a script which worked for someone in a similar thread (http://www.sitepoint.com/forums/showthread.php?t=30645):

    Code:
    <? session_start(); // initialize a session & declare at the very top ?> 
    
    <html> 
    <head> 
        <title>Untitled</title> 
    </head> 
    
    <body> 
    
    <?php 
    
    // register a session variable 
    session_register('counter'); 
    
    // increment and display the value of the counter 
    $counter++; 
    echo("You have visited this page $counter times! Don't you have anything 
    else to do, you bum?!"); 
    
    ?> 
    
    </body> 
    </html>
    Which gives me this error:

    Code:
    You have visited this page 1 times! Don't you have anything else to do, you bum?! 
    Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

  6. #6
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there...

    Try this... I've also put in an example of how to kill sessions...

    PHP Code:
    <?php
    session_start
    (); // initialize a session & declare at the very top 

    // check if $_SESSION['counter'] variable exists, if it does, increase it, if it does not, set it to 1
    if (isset($_SESSION['counter'])) {
        
    $_SESSION['counter']++;
    } else {
        
    $_SESSION['counter'] = 1;
    }

    // assign it to a friendly variable name. 
    $counter $_SESSION['counter'];

    // if the querystring "startagain" is set, then check if it is equal to 1 and if it is,<BR> destroy the session and display my attempt at immitating southpark chars. 
    if (isset($_GET['startagain'])) {
        if (
    $_GET['startagain'] == "1") { 
            
            
    // kill session
            
    session_destroy();
            echo 
    "You've killed the session...<BR>...<B>YOU *******S!</B><BR><BR>";
        }
    }
    ?> 

    <html> 
    <head> 
        <title>Untitled</title> 
    </head> 
    <body> 
    <?php
    echo("You have visited this page $counter times! Don't you have anything 
    else to do, you bum?!"
    ); 

    echo 
    "<BR><a href = \"sessions.php?startagain=1\">KILL THE SESSION!</a>";
    echo 
    " | <a href = \"sessions.php\">REVISIT PAGE!</a>";
    ?> 
    </body> 
    </html>

  7. #7
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parse error on line 6. Thanks anyhow.

  8. #8
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there...


    hmm... strange... it's my testing code... it worked for me... :/
    what's your line 5?

    did you copy and paste the code provided within the example?

    show me what you got... can you copy and paste code here?

  9. #9
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the error I got:

    Code:
    Parse error: parse error, unexpected '}' in e:\domains\d\mydomain.co.uk\user\htdocs\myfolder\sessiontest.php on line 6
    I just copied and pasted exactly what your code said into a .php page and tried to run it??

    I've actually managed to do what I wanted to do, using some kind of primative cookie system - it's crap, but nothing else I try seems to work, and I don't have time to learn the intricacies of PHP. At least the job is done now, albeit badly. Thanks for your help anyway.

  10. #10
    Non-Member melancholic's Avatar
    Join Date
    Nov 2004
    Location
    Australia
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh!
    At least the job is done now,
    such is the anthem of the modern day freelancer
    glad to see you got it workin somehow

    strange tho' There's no way a parse error can occur on one system and not occur on another. :/


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
  •