Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP Login User Redirect

Posted on 2010-09-07
3
253 Views
Last Modified: 2012-05-10
Hi Experts. I’m sure this is a pretty simple fix but I can’t seem to figure it out. I currently have a login form working so that if you login it will echo ‘success’ (this was mainly for testing) and if you login with the username ‘admin’ it will redirect you to the ‘admin’ specific page… This is all working. The only issue is with my error checking and validation. If I login with a test username and password it will check to make sure they match and are correct than echo success. If I login with ‘admin’ as the username it will redirect the user to the admin page… regardless of what password they enter. Is there a way to make it so that it checks ‘admin’ for the correct password before redirecting? Any help would be greatly appreciated.
<?php
ob_start();
session_start();

$username = $_POST['username'];
$password = md5($_POST['password']);

if ($username&&$password)
{
	
	$connect = mysql_connect("xxx", "xxx", "xxx") or die("Connection Error!");
	mysql_select_db("xxx") or die("Couldn't find db");
	
	$query = mysql_query("SELECT * FROM users WHERE username='$username'");
	
	$numrows = mysql_num_rows($query);
	
	if ($numrows!=0)
	{
		while ($row = mysql_fetch_assoc($query))
{
	$dbusername = $row['username'];
	$dbpassword = $row['password'];
}

//check to see if they match
if($username==$dbusername&&$password==md5($dbpassword))
{
	echo "Success!";
	$_SESSION['username']=$username;
}
else
echo "<br>Password is incorrect.<br><a href='#'>Back</a><br>";

	}
	
	else
	die ("<br>That user doesn't exist.<br><a href='#'>Back</a><br>");
	
}
else
die("<br>Please enter username and password.<br><a href='#'>Back</a><br>");


//ADD USER REDIRECTS HERE

if ($username=="xxx")
{
header("Location: xxx/index.php");
}


?>

Open in new window

0
Comment
Question by:brettsky07
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33620108
I would put the redirect just after the 'Success' and session variable so it was inside the condition checks instead of outside where it just takes a username if it gets that far which it is.
0
 

Author Comment

by:brettsky07
ID: 33620186
wow. cant beleive I didnt try that. haha that worked just fine thanks!
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 33620398
thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 39
PHP curl issue VERBOSE output 18 86
Echo Multiple values from multiple records 19 39
Reference key in foreach loop 4 18
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question