Jump to content

Login system help


elflacodepr

Recommended Posts

Hello all,

 

I need help with this login system, the problem is I don't know how to retrieve data correctly from DB I have an idea but still not working.

 

Also I'm not sure if arrays in login systems work same way I did....till blows my head everytime I work with them...any help is appreciated.

 

Here is the script:

<?php

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

$username = mysql_real_escape_string($_POST['username']);
$userpass = mysql_real_escape_string($_POST['password']);

include 'library/config.php';
include 'library/open.php';		


	// Query
	$sql = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'") or die ('Error, cannot continue');


	$row = mysql_fetch_array($sql);

	if(mysql_num_rows($sql) == 0)	
	die("<br />Invalid user or password!<br />");

	else

	session_start();
	$_SESSION["username"]=$row["username"];
	$_SESSION["username"]=$row["password"];
	header("LOCATION: admin/admin.php");


include 'library/closedb.php';


}
?>

Link to comment
Share on other sites

Try it like this, you've got some of the orders mixed around. And session_start() should ALWAYS be at the top. Oh and you were using $password not $userpass. :D

 

<?php
session_start(); //ALWAYS!!!

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

$username = mysql_real_escape_string($_POST['username']);
$userpass = mysql_real_escape_string($_POST['password']);

include 'library/config.php';
include 'library/open.php';		


	// Query
	$sql = mysql_query("SELECT * FROM `users` WHERE `username`='{$username}' AND `password`='{$userpass}'") or die ('Error, cannot continue');

	if(mysql_num_rows($sql) == 0){
	die("<br />Invalid user or password!<br />");

}else{

	$row = mysql_fetch_array($sql); //you can use array 


	$_SESSION['username'] = $row['username'];
	$_SESSION['username'] = $row['password'];
	header("LOCATION: admin/admin.php");


include 'library/closedb.php';


}
?>

Link to comment
Share on other sites

oh..all fixed thanks for that! :D

 

now the problem is, I get stuck in the validate script (which is the one of above) when I try to Login, if I fill the login info, it redirects me to the validating script even if the password is wrong, all it shows it a blank page ???

Link to comment
Share on other sites

ok, I have 2 files, the Login and Validate:

 

Login.php

<html>
<head>Login</head>
<body>
<table>
<tr>
	<td>
		<form action="validate.php" method="POST">
		User:
		<input type="text" name="username"><br />
		Password:
		<input type="password" name="password">
		<input type="submit" value="Login">
		</form>
	</td>
</tr>
</body>
</html>

 

Validate.php

<?php
session_start(); //ALWAYS!!!

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

$username = mysql_real_escape_string($_POST['username']);
$userpass = mysql_real_escape_string($_POST['password']);

include 'library/config.php';
include 'library/open.php';		


	// Query
	$sql = mysql_query("SELECT * FROM `users` WHERE `username`='{$username}' AND `password`='{$userpass}'") or die ('Error, cannot continue');


	if(mysql_num_rows($sql) == 0){	

	die("<br />Invalid user or password!<br />");

}else{

	$row = mysql_fetch_array($sql); //you can use array 

	$_SESSION['username'] = $row['username'];
	$_SESSION['username'] = $row['password'];
	header("LOCATION: admin/admin.php");



include 'library/closedb.php';


}

}

?>

 

What I mean is, after I fill the login info(username and password) it is supposed to check all info using Validate.php script, once is checked and everything is correct it must redirect me to "admin/admin.php". but it doesn't, it simply shows a blank page after I click the Login button(inlogin.php)

 

 

I hope I could make ya the map  :(

 

 

 

so u want it to show an error if the password is wrong?

Yes thats what I want, but it doesn't work at all, as I can type anything and it redirects me  ???

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.