Jump to content

[SOLVED] Basic Login Script help


Ancoats

Recommended Posts

Hey guys, running into problems when trying to create a basic login script. The script seems to run fine but doesnt acknowledge any of the usernames etc in the tables when I go to login? Was wondering what I've done wrong.

 

The table details (as entered in when creating it is:)

 

 

$tablename = "members";

$create = "CREATE TABLE members (

  memberID int(4) NOT NULL auto_increment,

  firstname varchar(20) NOT NULL default '',

  lastname varchar(20) NOT NULL default '',

username varchar(20) NOT NULL default '',

password char(32) NOT NULL default '',

  email varchar(255) NOT NULL default'',

  PRIMARY KEY (memberID)

)

ENGINE=MyISAM";

 

 

(this table example works fine with the registration script which successfully enters a registered member into the table so I assume this is not the problem)

 

 

My login code is:

 

<?php

//start the session

session_start();

 

// db properties

$dbhost = 'localhost';

$dbuser = 'xxx';

$dbpass = 'xxx';

$dbname = 'xxx';

 

// make a connection to mysql

$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());

mysql_select_db ($dbname) or die ("I cannot select the database '$dbname' because " . mysql_error());

 

//If slogin is set then the form has been sent

if(isset($_POST['slogin'])){

 

//Make the data safe

$username = trim($_POST['username']);

$username = strip_tags($username);

$username = mysql_real_escape_string($username);

 

$password = trim($_POST['password']);

$password = strip_tags($password);

$password = mysql_real_escape_string($password);

$password = md5($password);

 

 

//check if the user ID and password combination exist in db

$sql = "SELECT * FROM members WHERE username = '$username' AND password = '$password'";

$result = mysql_query($sql) or die ('Query failed. ' . mysql_error());

 

 

//the username and password match, set the session

if (mysql_num_rows($result) == 1) {

 

//get member ID from the database

while ($row = mysql_fetch_object($result)){

 

//assign memberID to a variable

$memberID = $row->memberID;

 

//set the session

$_SESSION['isloggedin'] = $memberID;

}

//reload the page

header('Location: '.$_SERVER['HTTP_REFERER']);

exit;

 

}else{

echo "Your a fake";

}

}

?>

 

 

The Login box code where the user would enter in the code is:

 

<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">

 

<p>Login: Username

 

<input name="username" type="text" size="10" />

 

Password

 

<input type="password" name="pass" size="10" />

 

<input type="submit" name="slogin" value="Login" />

 

</p>

 

</form>

 

 

The error I am getting is that the script runs til the end and when the data has been entered into the login boxes, the echo of "Your a fake" comes up.

 

So where am I going wrong??  ??? Help would be appreciated, thanks  ;D

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.