Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Using session data

    I'm am trying to use sessions to send user data between web pages on my site.
    The page starts by receiving form information from another page:
    Code:
     $name = $_POST['userid'];
    It then uses that data to make the session variable "$session['name']":
    Code:
    session_start() ;
    $session['id']=session_id();
    $session['name']= $name;
    The problem here is that I want to be able to use the variable: "$session['name']" on another page but don't really know how. Any help would be appreciated. Thanks!

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Well firstly you could use a predefined variable to store it like $_SESSION... so like
    PHP Code:
    session_start();
    $_SESSION['id'] = session_id();
    $_SESSION['name'] = $name
    and if I remember as long as session_start is called every time you need the data it'll be their unless they clear it. So you'll probably want a check to make sure they haven't cleared it somehow.. anyhow good luke.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    6
    Thanked 0 Times in 0 Posts
    This is my page which tries to draw the session data:
    PHP Code:
    <?php 
    session_start
    ();
    $session['id']=session_id();
    $name $session['name'];

    echo 
    $name;
    ?>
    You might be right about it clearing the data but I can't see where it would be doing so. Here is a bit more of the code from the session start page:
    PHP Code:
    if ($psswrd == $password)
    {
    session_start() ;
    $session['id']=session_id();
    $session['name']= $name
    echo 
    "<p class=data> <center>Successfully,Logged in<br>
    <br><a href='logout.php'> Log OUT </a><br>
    <br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>"
    ;
    }
    else { 
    session_unset();
    echo 
    "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Userid and Password and Try <br><center>
    <input type='button' value='Retry' onClick='history.go(-1)'></center>"
    ;}



  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    $_SESSION['id'] = session_id();

    $session['id']=session_id();

    These are not the same things.

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    6
    Thanked 0 Times in 0 Posts
    This still isn't working, I believe that this is because the sessions are not setting correctly. I found this session test page from a tutorial website:
    PHP Code:
    <?php
    session_start
    ();  
    if(isset(
    $_SESSION['views']))
        
    $_SESSION['views'] = $_SESSION['views']+ 1;
    else
        
    $_SESSION['views'] = 1;

    echo 
    "views = "$_SESSION['views']; 
    ?>
    When I refresh the page it should increase the page view, this, however, doesn't happen. I think what is happening is that the page is thinking that the session is not set and therefore moving on to the "else" clause down below. If anyone knows anything that could help me then please say so as I am totally lost at this point.
    Thanks again.

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    It could simply be that your host has disabled Sessions. To check this you need to view your PHP.INI file by creating a PHP file with the code:

    PHP Code:
    <?PHP
    echo phpinfo()
    ?>
    Then open the page in your browser and scroll down to Session the first field "Session Support" should hopefully say enabled... otherwise it is disabled, and you won't be able to use them.


    ~Jordan
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Just a quick tip, no need to use echo for phpinfo, as the function already echo's stuff out. Just

    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    is enough

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    25
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Session support is enabled so that doesn't seem to be the problem. At one point I was able to get a session test to work however I was not able to repeat this test. The problem could lie somewhere in my code but after checking and rechecking I don't see a problem.
    Here is the page which starts the session:
    PHP Code:
    <html>
    <head>
    <?php


    $name 
    $_POST['userid'];
    $password $_POST['password'];

    $flag="OK";  
    $msg="";       

    if(
    strlen($name) < 5){   
    $msg=$msg."( Please enter user id more than 5 character length  )<BR>";
    $flag="NOTOK";  
    }

    if(
    strlen($password) < ){    
    $msg=$msg."( Please enter password of more than 5 character length  )<BR>";
    $flag="NOTOK";  
    }

    if(
    ereg('[^A-Za-z]'$name)){   
    $msg=$msg."( Please use only alphabets a to z as userid   )<BR>";
    $flag="NOTOK";   
    }

    if(
    $flag <>"OK"){
    echo 
    "<center>$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'></center>";
    }else{ 

    mysql_connect("XXXXX""XXXXX""XXXXX") or die(mysql_error());
    mysql_select_db("stats") or die(mysql_error());

    $result mysql_query("SELECT * FROM $name") or die(mysql_error());

    $row mysql_fetch_array($result);

    $psswrd $row['password'];

    if (
    $psswrd == $password)
    {
    session_start() ;
    $_SESSION['name'] = $name
    echo 
    "<p class=data> <center>Successfully,Logged in<br>
    <br><a href='logout.php'> Log OUT </a><br>
    <br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>"
    ;
    print 
    "<script>";
    print 
    " self.location='welcome.php';"
    print 
    "</script>";
    }
    else { 
    session_unset();
    echo 
    "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct Userid and Password and Try <br><center>
    <input type='button' value='Retry' onClick='history.go(-1)'></center>"
    ;}


    }
    ?>
    </head>
    <body>
    </body>
    </html>
    And here is the page which is supposed to display the session variable:
    PHP Code:

    <?php 
    session_start
    ();


    echo 
    "hello";
    echo 
    $_SESSION['name'];


    ?>
    Once again any help would be appreciated.

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    session_start(); is meant to be before any output to the browser. You have it near the end of the page


  •  

    Posting Permissions

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