Login php sessions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Quinonez

    #1

    Login php sessions

    Im having a problem, which in my head seems to be simple but i cannot
    figure it out.

    there are 3 pages.

    Page 1 (login form)

    page 2 (verifies login/pass and begins session. has link to page 3)

    Page 3 (displays some info has a link back to page 2)

    obviously i only want pages 2 and 3 to be able to be accessed through
    the login.

    i can get to page 2 and page 3 after login no problem but IF i press
    the link on page 3 to page 2 it takes me back to page 1 and i have to
    login again.

    here is the code

    Page 1 (admin.php)
    ......

    <form method="post" action="edit_si te.php">
    Name: <input type="text" name="username" >
    Password: <input type="text" name="password" >
    <input type="submit" value="ENTER">
    </form>
    ......

    Page 2 (edit_site.php)

    <?php
    session_start() ;

    $db_user = 'database_user' ;
    $db_pass = 'database_passw ord';
    $username = $_POST['username'];
    $password = $_POST['password'];


    $connection = mysql_connect(' localhost', $db_user, $db_pass) or
    die(mysql_error ());
    mysql_select_db ('database_main ', $connection) or die(mysql_error ());


    $query = "SELECT * FROM users
    WHERE username='$user name' AND password='$pass word'";

    $result = mysql_query($qu ery, $connection) or die('error making
    query');
    $affected_rows = mysql_num_rows( $result);

    if($affected_ro ws == 1) {
    $_SESSION['username'] = $username;
    }
    else {
    header( "Location: admin.php" );
    }

    ?>

    Page 3 (edit_news.php)


    <?php
    session_start() ;
    if(session_is_r egistered('user name')){

    echo 'Welcome, you are still logged in.';

    }
    else{


    header( "Location: admin.php" );
    }
    ?>

    any help is GREATLY appreciated. thank you

  • MsKitty

    #2
    Re: Login php sessions

    You need to register the session variable before you set it, thus:
    session_registe r("username") ;
    $_SESSION['username'] = $username;


    Kitty
    San Diego web design, development, training, and programming - we set up membership sites, Blogs, Ecommerce, Dynamic Content, and easily maintained web sites


    Comment

    • Quinonez

      #3
      Re: Login php sessions

      i entered that here

      Page 2 (edit_site.php)
      .....
      if($affected_ro ws == 1) {
      session_registe r("username") ;
      $_SESSION['username'] = $username;
      }
      else {
      header( "Location: admin.php" );
      }

      ?>

      doesnt work? is that correct, Shouldnt there be something when i come
      back that checks if the session is still good?

      Comment

      Working...