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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post

    Exclamation Session Variables Resetting Due to Code - Help Please

    Hi,

    I am currently coding a Blog for multiple users and I am having problems with session variables.

    Here is my problem;

    I have several pages for logging in and then an index.
    No matter what you are (guest, admin, mod or user) there is always session variables available for the server to use to make my site work.
    Here are the default values of the session variables (these are used for guests)

    PHP Code:
    $_SESSION['skin'] = "1";
    $_SESSION['access'] = "0";
    $_SESSION['username'] = "Guest";
    $_SESSION['group'] = "0"
    That's all fine...
    Once I log in it changes the variables depending on the user, so for example
    I log in as admin
    PHP Code:
    $_SESSION['skin'] = "1";
    $_SESSION['access'] = "1";
    $_SESSION['username'] = "admin";
    $_SESSION['group'] = "1"
    The 'access' variable tells the web pages that they are logged in
    'group' is the user group and permission (administrators are valued at 1)

    Once I visit my index page all the variables reset because the variables are reset but...
    How can I get it to keep the logged in user's variables to stay??

    Thanks,
    Dacoder96.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Firstly are you using session_start()
    secondly, you say that those session variables are the defaults, are you checking if they're set already before assigning those?
    For examplee
    PHP Code:
    <?php
    session_start
    ();
    if(!isset(
    $_SESSION['access'])) {
        
    $_SESSION['skin'] = "1"
        
    $_SESSION['access'] = "0"
        
    $_SESSION['username'] = "Guest"
        
    $_SESSION['group'] = "0"
    }
    That will only set the variables if $_SESSION['access'] isn't set
    Also, what is the time limit on your sessions? It could be that you are running out of time (although unlikely)
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yes, I'm using session_start()

    Time limit, um...
    i got this;
    Timeouts Connection: 10 - Keep-Alive: 5
    and
    session.gc_maxlifetime 1440 1440

    So just doing $_SESSION[' '] = ""; isn't setting the variable?
    Last edited by dacoder96; 01-24-2010 at 02:10 AM. Reason: link was blanked out

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you show the code that sets the session vars and the code that checks it we can verify where you're going wrong. Yes it will be in the phpinfo under session cookie lifetime
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    ok

    just incase ill post the login page code
    this is the form the login page uses

    login.php
    Code:
    <form action="loginAction.php" method="post" name="login">
      <table width="298" cellspacing="1" cellpadding="1">
        <tr>
          <td height="25"><p>Username:</p></td>
          <td height="25"><input name="username" type="text" size="25" maxlength="16" class="loginRegisterForm" /></td>
        </tr>
        <tr>
          <td height="25"><p>Password: </p></td>
          <td height="25"><input name="password" type="password" size="25" maxlength="16" class="loginRegisterForm"  /></td>
        </tr>
        <tr>
          <td><input name="login2" type="submit" value="Login" class="loginRegisterForm"  /></td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </form>
    now it goes to loginAction.php
    loginAction.php
    PHP Code:
    $myusername $_POST['username'];
    $mypassword $_POST['password'];
    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    $sql "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    if(
    $count==1){
        include(
    'loginSuccess.php');
    }
    else {
        
    header('location:login.php?error=1');

    yes, i realise there is no sql connection but that is on a seperate page. dont worry, i got that working

    now it goes to loginSuccess.php if its all ok
    loginSuccess.php
    PHP Code:
    $_SESSION['access'] = "1";
    $_SESSION['group'] = "1";
    $_SESSION['username'] = "admin";
        echo(
    'logged in'); 
    then i go back to index (as u do) and all the session variables go back to...
    PHP Code:
    $_SESSION['skin'] = "1";
    $_SESSION['access'] = "0";
    $_SESSION['username'] = "Guest";
    $_SESSION['group'] = "0"
    that code is on everypage

  • #6
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    if(!($_SESSION['access'] == "1")){
       
    $_SESSION['skin'] = "1";
       
    $_SESSION['access'] = "0";
       
    $_SESSION['username'] = "Guest";
       
    $_SESSION['group'] = "0";  

    put that instead so if you're logged in we won't set the session vars over top of the login set vars.

  • #7
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    ahh, thank you! so obvious! that should work (hopefully)
    Thanks everyone for your help! much appreciated.


    ----------------------------------------------------------------------------------------------------

    second post;

    ok I got it working but there is still 1 variable that isn't staying! its the access one, i dont know why but here is some updated codes

    loginAction.php this is now the main login script - there is no more loginSuccess.php
    PHP Code:
    <?php
    $myusername 
    $_POST['username'];
    $mypassword $_POST['password'];
    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    $sql "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    if(
    $count==1){
        
    //$_SESSION['group'] = "1";
        
    $_SESSION['username'] = $myusername;
        echo(
    'Skin: '.$_SESSION['skin']. '<br>');
        echo(
    'Access: '.$_SESSION['access']. '<br>');
        echo(
    'Username: '.$_SESSION['username']. '<br>');
        echo(
    'Group: '.$_SESSION['group']. '<br>');
        
    $_SESSION['access'] = '1';
        
    header('location: index.php');
    }
    else {
        
    header('location: login.php?error=1');
    }
    ?>
    this works fine from what i can see

    now it redirects u to index.php instantly
    the contents of index.php isn't anything yet so im not posting it

    i think this is our problem... i use global headers and footers to create the webpages and my global header might be messing it up
    here is the code
    globalHeader.php
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['skin'] = "1";
    if (
    $_SESSION['access'] = '0') {
    $_SESSION['skin'] = "1";
    $_SESSION['username'] = "Guest";
    $_SESSION['group'] = "0";
    }

    $host=""// Host name
    $username=""// Mysql username
    $password=""// Mysql password
    $db_name=""// Database name
    $tbl_name=""// Table name

    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if (
    $_SESSION['skin'] != "0") {
        include(
    "skins/".$_SESSION['skin']."/pageHeader.php");
    }
    include(
    'navigation.php');
    ?>
    i am spotting the error out by this;
    i am writing all the values of the variables to each page so i can see wat they currently are using this code
    PHP Code:
    echo('Skin: '.$_SESSION['skin']. '<br>');
    echo(
    'Access: '.$_SESSION['access']. '<br>');
    echo(
    'Username: '.$_SESSION['username']. '<br>');
    echo(
    'Group: '.$_SESSION['group']. '<br>'); 
    and when u r logged in the navigation should have profile and logout for options NOT login and register
    to do this im using this code
    navigation.php
    PHP Code:
    <p ="navigation"><a href="index.php">Home</a> | 
    <?php 
    session_start
    ();
    $_SESSION['username'] = "guest";
    $_SESSION['access'] = "0";
    if(!
    $_SESSION['username'] = 'guest' || !$_SESSION['access'] = "0") { 
    echo(
    '<a href="profile.php">Profile</a> | <a href="logout.php">Logout</a>'); 


    else 

    echo(
    '<a href="login.php">Login</a> | <a href="register.php">Register</a>'); 


    ?> 
    </p>
    Last edited by dacoder96; 01-24-2010 at 08:09 AM. Reason: so i dont double post

  • #8
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    the part where you have


    PHP Code:
    if($_SESSION['access']="0"){
    ...

    is actually setting access to 0 and then executing what's inside the if statement.

    if you want to check the value of access you need this

    PHP Code:
    if($_SESSION['access']=="0"){
    ...

    And I thought about it. Also, about what I said before I think maybe you should check if username is set. Something like

    PHP Code:
    if(isset($_SESSION['username'])){
    //If they logged in.
    }else{
    //If they didn't.

    etc.

    Good luck.

  • #9
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    so whats the difference between = and ==?
    is the single symbol just equal to even if there is capitalize differences (eg; test = Test = true)
    is the double symbol mean exactly equal to (eg; test = test = true BUT test = Test = false)

    yet again, thank you for your help!

  • #10
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    = means to set the value and == means to test the equality of the values.

    http://us.php.net/manual/en/language.types.boolean.php

  • #11
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    ok thanks
    now that i got that working i need to be able to pull data from my database (sql)
    Each user will have an option to which skin they want - there selection is stored in the database but I can't get it to pull the data back from the db
    here is my code - wats wrong with it? please help

    PHP Code:
        $_SESSION['username'] = $myusername;
        
    $sqlSkin mysql_query("SELECT 'skin' FROM 'accounts' WHERE 'username'={$_SESSION['username']}");
        
    $_SESSION['skin'] = mysql_fetch_array($sqlSkin); 
    ive googled it abit and I read that you put {} around the session variable and then ' ' around the names of columns in the db

  • #12
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I suspect it's because you're trying to set one value 'skin' to an entire array.
    try adding the name of the field to the end of the variable.

    Like this maybe,

    PHP Code:

    $skin_array 
    mysql_fetch_array($sql_query);
    $_SESSION['skin'] = $skin_array['skin_name'];

    //Where the skin is stored in the SQL database as skin_name. 

  • #13
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    so $sql_query is my $sqlSkin?
    PHP Code:
    $sqlSkin mysql_query("SELECT 'skin' FROM 'accounts' WHERE 'username'={$_SESSION['username']}"); 
    so the code would look like this...
    PHP Code:
    $sqlSkin mysql_query("SELECT 'skin' FROM 'accounts' WHERE 'username'={$_SESSION['username']}");
    $skin_array mysql_fetch_array($sqlSkin);
    $_SESSION['skin'] = $skin_array['skin']; 
    PHP Code:
    $_SESSION['skin'] = $skin_array['skin_name'];
    //Where the skin is stored in the SQL database as skin_name. 
    - replaced with 'skin' because that is the column in the db

  • #14
    New Coder
    Join Date
    Jan 2010
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts
    as long as each username only has one skin that should work. try it out and let me know how it goes. for future reference you should start a new thread next time in case someone else has this problem

  • #15
    New Coder
    Join Date
    Jul 2009
    Location
    Most of the time - internet
    Posts
    85
    Thanks
    0
    Thanked 1 Time in 1 Post
    ok sorry - for convience i thought i would just continue this thread
    - just quickly, its not working. its still not working... it appears it isn't even receiving the value
    Last edited by dacoder96; 01-25-2010 at 01:30 AM.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

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