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 2 of 2

Thread: session help

  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    session help

    I have a login page that check the user name and password from the db. If the username and password are valid it call a page which the redirects the user based on the username. Problem is, is that I have to login 2 times for it to take, the first time I login it kick me out to the invalid user name and password, the second time I login it work.... any ideas?

    Login Script

    PHP Code:
    <?php
    $host
    ="localhost"// Host name 
    $username="name"// Mysql username 
    $password="password"// Mysql password 
    $db_name="users"// Database name 
    $tbl_name="users"// Table name 

    // Connect to server and select databse.
    mysql_connect("$host""$username""$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");

    session_start();
    $username $_POST['username'];
    $password $_POST['password'];
    $query "select * from users where username='$username' and password='$password'";
    $result mysql_query($query);
    if (
    mysql_num_rows($result) != 1) {
    $error "Bad Login";
    include 
    "login.php";
    } else {
    // set a cookie      
    $_SESSION['username']=$username;
    mysql_query("UPDATE users SET last_login=now() WHERE username='$username'");
    mysql_query("UPDATE users SET visits=visits+1 WHERE username='$username'");
    header("Location: up/x.php"); 
    exit; 

    ?>
    X.php

    PHP Code:
    <?      
    // members page      
    session_start(); 
    if (
    $username =='tom')
    include 
    'cams.php';
    elseif (
    $username =='mike')
    include 
    'cams1.php';
    elseif (
    $username =='john')
    include 
    'cams2.php';
    elseif ( empty( 
    $username ) ) {           
    include 
    'error.htm';
    }
    ?>
    cams.php cam1s.php cams2.php
    PHP Code:
    <?      
    // members page      
    session_start(); 
    print 
    "Hello $username.";     
    if ( empty( 
    $username ) ) {           
    include 
    'error.htm';      
    } else {      
    ?>
    Last edited by brentc73; 11-13-2006 at 09:50 PM.

  • #2
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your if statement may be doing it...
    try this

    $row = mysql_fetch_assoc($result);

    if ($row['username'] != $username || $row['password'] != $password) {
    //echo error
    //show login.php
    } else {
    //register session
    }


  •  

    Posting Permissions

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