View Full Version : session help

11-13-2006, 08:47 PM
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

$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");

$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
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");


// members page
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

// members page
print "Hello $username.";
if ( empty( $username ) ) {
include 'error.htm';
} else {

11-13-2006, 09:43 PM
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