www.webdeveloper.com
Results 1 to 8 of 8

Thread: Php login help

  1. #1
    Join Date
    Jul 2012
    Posts
    57

    Php login help

    Ok, how is this not working?

    check file (skipped over some variables:

    $sql = "SELECT * FROM $tbl_name WHERE username='$username' AND password='$password' LIMIT 1";
    $run = mysql_query($sql);
    $count = mysql_num_rows($run);

    if ($count > 0) {
    session_start();
    $_SESSION['user'] = $username;
    header('Location: apage.php');
    die();
    }
    else {
    header('Location: home.php');
    die();
    }

    //It doesn't work?!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,523
    Sometimes (and I'm not really sure why), you need to do a session_write_close() before your call to your header() redirect (but after you're all done accessing $_SESSION). You might give that a try and see if it helps.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2012
    Posts
    57
    Ok... still having the issue, but to make the situation more of a big deal, check this out:

    my protected page if the check file worked:

    <?php

    require("check.php");

    if(!isset($_SESSION['user']))
    {
    session_save_path('/apathiuse');
    session_start();
    session_write_close();
    header('Location: home.php');
    }

    ?>

    And I get:
    0
    Internal Server Error.. blah blah blah

    Im hosting with ipage.. I'm assuming now the problem resides outside of the code? Or am I blind?

  4. #4
    Join Date
    Aug 2012
    Posts
    155
    session_start() always has to come at the very top of all other code (php or html) so move that out of your if statement to the top of your code and then try your original code again (I have also changed die() to exit()).
    PHP Code:
    <?php
    session_start
    ();
    // Other code here that you have removed for your question
    $sql "SELECT * FROM $tbl_name WHERE username='$username' AND password='$password' LIMIT 1";
    $run mysql_query($sql);
    $count mysql_num_rows($run);

    if (
    $count 0) {
    $_SESSION['user'] = $username;
    header('Location: apage.php');
    exit();
    }
    else {
    header('Location: home.php');
    exit();
    }

  5. #5
    Join Date
    Jan 2013
    Posts
    3
    Quote Originally Posted by simplypixie View Post
    session_start() always has to come at the very top of all other code (php or html) so move that out of your if statement to the top of your code and then try your original code again (I have also changed die() to exit()).
    PHP Code:
    <?php
    session_start
    ();
    // Other code here that you have removed for your question
    $sql "SELECT * FROM $tbl_name WHERE username='$username' AND password='$password' LIMIT 1";
    $run mysql_query($sql);
    $count mysql_num_rows($run);

    if (
    $count 0) {
    $_SESSION['user'] = $username;
    header('Location: apage.php');
    exit();
    }
    else {
    header('Location: home.php');
    exit();
    }
    im agree this. on php without framework, u need create session start always on top.

  6. #6
    Join Date
    Jul 2012
    Posts
    57
    Ok thanks,

    now how exactly is session working? Do I need to call the page in which the check process happens on my protected page? I've been playing around with different ways and I seem to lack in luck. How does php handle this process is more so what I'm asking.

    Edit: currently, on my protected page I just use require(checkpage).. and no error report.. however it's forgetting the user logged in I think because I get redirected to login page. Any tip?
    Last edited by jazzmasterkc; 01-05-2013 at 05:26 PM.

  7. #7
    Join Date
    Mar 2011
    Location
    The Pleiades
    Posts
    31
    After setting a session, if you locate to another page using a header() call, you need to restart the session in the page which you go to.

    Consider this code:
    Code:
    session_start();
    
    $_SESSION['name'] = "LearningCoder";
    
    header("Location: file.php");
    and now this is the file which I locate to with a header() call:
    Code:
    session_start();
    echo $_SESSION['name'];
    OK. Load up code number one - call it whatever you like. Rename the second code as file.php

    Notice, when you load up the first code, you locate to the file.php page and the session displays.......

    Now take out the session_start() from the second code...

    Notice you get the undefined error.

    Just add session_start() into your second code.

    Regards,

    LC.

  8. #8
    Join Date
    Jul 2012
    Posts
    57
    Ah ha! got it, I figured out the session acts more so like a cookie and not required to require check page

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles