Results 1 to 12 of 12

Thread: PHP session problems - variables not holding from page to page

  1. #1
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    PHP session problems - variables not holding from page to page

    I was making a website for someone with a basic login system. I made it, and it was working fine for months, until now. User logs in, user/pass checked with the database, session variable set to "loggedin" or whatever, and then headers the user to the "private" page.

    Now, for some reason, the pages just aren't saving the session variables so when the private page checks if the user is logged in (calls this function):

    Code:
    function logincheck()
    {
    if (!isset($_SESSION['userloggedin']))
    {
    header("Location: non private index");
    }
    }
    And it immediately links me back to the main page.

    Having removed the check to see what was going on, i am confronted with a myriad errors claiming that loops are incorrectly done, divisions by zero all over the shop, etc.

    From a little fiddling, it is apparent that the session data isn't being carried. All the pages have the usual
    Code:
    <?php
    session_start();
    snippet, and as i said, it was working great up until today.

    Any ideas about what could be up? I was wondering if it's my hosting company's fault - QiQ, or whether it's a database problem (unlikely i expect and i can't see how it would screw up sessions). I've checked phpinfo() and sessions are enabled - as they should be.

    The weirdest thing is that i can try with a really simple test case like:

    Code:
    <?php
    session_start();
    $_SESSION['test'] = "hi";
    header(location:"2.php");
    ?>
    
    2.php
    <?php
    session_start();
    echo $_SESSION['test'];
    ?>
    And it'll output "hi" like it should.

    Any ideas on what could be causing the error?

    EDIT:
    This is the login bit that headers to the private page:

    Code:
    elseif ($num == 1)
    {
    			$sql = "SELECT * FROM stats WHERE username = '$username'";
    			$result = $db->query($sql);
    			$row = $db->fetcharray($result);
    						
    			$_SESSION['userloggedin'] = 1;
    			$_SESSION['userid'] = $row['id'];
    			
    			header("Location: Private Site");
    Obviously the login page also has session_start(); at the top..

  2. #2
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP session problems - variables not holding from page to page

    You should have a LIMIT 0,1 on your SELECT * query.

    First off why not echo the value of $_SESSION['userloggedin']/$_SESSION['userid'] to the screen or to a log file to see where it should be set/where it's becoming unset.

    Try turning E_STRICT and E_NOTICE on to see if it's variable/structure accessor problem. As for why it would suddenly break, has your hosting company recently upgraded anything on the server. PHP for instance? What about these errors? Presumably they are errors caused by the fact that scripts on that page are reliant on the user being logged in/session data available.
    To err is human. To really foul things up ... you need a computer.

  3. #3
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: PHP session problems - variables not holding from page to page

    Ok i've fiddled for most of today and i've found an interesting problem..

    Login form -> Login script -> Private page = session not transferred

    Login script directly (with the login details in the script) -> Private page = fine

    That's weird if you ask me, somewhere along the line, using a form to access the script doesn't work, yet just running the script directly does?!

  4. #4
    Custom User Title
    Join Date
    Oct 2005
    Location
    Wirral UK
    Posts
    1,168
    Thanks
    10
    Thanked
    14 times in 14 posts
    • cougarslam's system
      • Motherboard:
      • Asus Maximus Formula SE (ROG)
      • CPU:
      • Core 2 Duo E6600 @ 3ghz
      • Memory:
      • 4gb Corsair DDR2
      • Storage:
      • 1TB
      • Graphics card(s):
      • BFG Nvidia 8800GT OC 512MB
      • PSU:
      • Corsair HX520
      • Case:
      • Zorro
      • Operating System:
      • Vista Business 32
      • Monitor(s):
      • 2 x 17" crt
      • Internet:
      • adsl max (entanet)

    Re: PHP session problems - variables not holding from page to page

    I have a qiq php website and their configuration caused me so many issues that i do not use it.

  5. #5
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP session problems - variables not holding from page to page

    Sounds like whatever code to check for form submission isn't executing, or vars from that form aren't being read in correctly.
    To err is human. To really foul things up ... you need a computer.

  6. Received thanks from:

    Whiternoise (09-03-2008)

  7. #6
    Custom User Title
    Join Date
    Oct 2005
    Location
    Wirral UK
    Posts
    1,168
    Thanks
    10
    Thanked
    14 times in 14 posts
    • cougarslam's system
      • Motherboard:
      • Asus Maximus Formula SE (ROG)
      • CPU:
      • Core 2 Duo E6600 @ 3ghz
      • Memory:
      • 4gb Corsair DDR2
      • Storage:
      • 1TB
      • Graphics card(s):
      • BFG Nvidia 8800GT OC 512MB
      • PSU:
      • Corsair HX520
      • Case:
      • Zorro
      • Operating System:
      • Vista Business 32
      • Monitor(s):
      • 2 x 17" crt
      • Internet:
      • adsl max (entanet)

    Re: PHP session problems - variables not holding from page to page

    maybe check your posts, sessions etc
    print_r($_POST);
    print_r($_SESSION);

  8. Received thanks from:

    Whiternoise (09-03-2008)

  9. #7
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: PHP session problems - variables not holding from page to page

    @Yamangman

    The post variables are definitely being read fine, since i can set session variables based on them.

    @cougarslam

    Post is fine, session is fine on the login page, but clears when i try to go to the private page

  10. #8
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: PHP session problems - variables not holding from page to page

    Ok thanks for all your help guys.. I got it working by rewriting the form and the login page - somehow it now just works. But i still have no idea why it wouldn't before, i asked two friends of mine - both proficient PHP coders - and neither of them could work it out either..

  11. #9
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP session problems - variables not holding from page to page

    useful link for a good few tips for debugging PHP:
    ONLamp.com -- PHP Debugging Basics

    Or you could use something called an IDE, Java has some support, but us plucky .Net developers have wounderfully powerful remote debugging solutions (right down to even inside SQL server) and of course full debugging of the websites. You can breakpoint, change values, change code, and continue the execution of it.

    it makes maintaning code, like this much easyer, and using phlanger, you can port PHP to .Net easy.

    PHP isn't a particually good platform, examples like this demonstraite how timewasting it can be.
    throw new ArgumentException (String, String, Exception)

  12. #10
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: PHP session problems - variables not holding from page to page

    There are few PHP debuggers and IDE's out there. Eclipse PDT + Zend debugger is a good choice.

    I don't think this is a good example of how 'timewasting PHP is' at all, not even the slightest. Sounds more like an example of inexperience to me.
    To err is human. To really foul things up ... you need a computer.

  13. #11
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: PHP session problems - variables not holding from page to page

    indeed, like you've not seen the light of a modern IDE

    you can't say debugging this problem (which is much less likely to happen on .Net) was an effecient process.

    Remeber i'm somewhat of a language slag, and have written a fair few PHP based sites for beer money as a student.
    throw new ArgumentException (String, String, Exception)

  14. #12
    Pseudo-Mad Scientist Whiternoise's Avatar
    Join Date
    Apr 2006
    Location
    Surrey
    Posts
    4,274
    Thanks
    166
    Thanked
    386 times in 233 posts
    • Whiternoise's system
      • Motherboard:
      • DFI LANPARTY JR P45-T2RS
      • CPU:
      • Q6600
      • Memory:
      • 8GB DDR2
      • Storage:
      • 5.6TB Total
      • Graphics card(s):
      • HD4780
      • PSU:
      • 425W Modu82+ Enermax
      • Case:
      • Silverstone TJ08b
      • Operating System:
      • Win7 64
      • Monitor(s):
      • Dell 23" IPS
      • Internet:
      • 1Gbps Fibre Line

    Re: PHP session problems - variables not holding from page to page

    Hah i tried Zend once but these pages aren't that complicated and i'd rather just flesh it out in Notepad++ and the errors the site gives

    And inexperience maybe, but can you give me a reason why loading a page through a form somehow stops session data from being stored when it's fine if the script is accessed directly?
    Last edited by Whiternoise; 13-03-2008 at 09:09 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My experience with Windows XP Pro x64 Edition
    By Paul Adams in forum Software
    Replies: 7
    Last Post: 20-04-2007, 11:59 PM
  2. 8800GTX driver page problems
    By acrobat in forum PC Hardware and Components
    Replies: 7
    Last Post: 09-01-2007, 02:38 AM
  3. Holding page
    By Nick F in forum Software
    Replies: 9
    Last Post: 14-05-2006, 06:06 PM
  4. PhP Warnings at the top of every page
    By herulach in forum Help! Quick Relief From Tech Headaches
    Replies: 1
    Last Post: 11-12-2005, 11:31 PM
  5. Having problems writing to mySQL using PHP
    By Ramedge in forum Software
    Replies: 2
    Last Post: 12-10-2005, 11:42 PM

Posting Permissions

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