Page 1 of 1
Redirection[SOLVED]
Posted: Fri Jun 27, 2008 12:04 pm
by james3302
I have this code and read that I cannot anythign above the Header() function but how am I supposed to do that?
Code: Select all
<?php
require 'DBConnect.php';
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT username, password FROM users WHERE username='$username' and PASSWORD('$password')='$password'";
$result = mysql_query($query);
if(mysql_num_rows($result)<> 1)
echo "<center><h2>$username is now Logged in!</h2></center>";
else
header("Location: login.html");
?>
I have to call another php file(not sure how to do that) just to have it run the header funtion.
Oh and you can find a better way to validate the username and password that would be great, not sure how reliable this is.
Re: Redirection
Posted: Fri Jun 27, 2008 12:28 pm
by Frozenlight777
not sure what you mean with the header function, but you may want to do a mysql_real_escape_string on the on the username and maybe md5 the password (or some other encryption method)
Re: Redirection
Posted: Fri Jun 27, 2008 2:02 pm
by james3302
Well if the username/password is incorrect I want them redirected to the login page again.
Also how can I validate the password. I'm using Password('$password') to write the password to the DB.
Re: Redirection
Posted: Fri Jun 27, 2008 3:58 pm
by james3302
Code: Select all
<?php
require 'DBConnect.php';
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "SELECT username, password FROM users WHERE username='$username'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if($username == $row['username'])
if($password == $row['password'])
echo "<h2>$username is logged in!<br>";
else
//I would like to redirect them back to the login page here!
echo "<h2>Incorrect username and/or password</h2>";
}
?>
Updated code I can now login correctly. see the comment about wanting to redirect. I cannot use Header( ) here.
Warning: Cannot modify header information - headers already sent by (output started at /home/whatith/public_html/Examples/DBConnect.php:10) in /home/whatith/public_html/Examples/login.php on line 18
Re: Redirection
Posted: Fri Jun 27, 2008 4:07 pm
by evinowen
james3302 wrote:
Updated code I can now login correctly. see the comment about wanting to redirect. I cannot use Header( ) here.
Warning: Cannot modify header information - headers already sent by (output started at /home/whatith/public_html/Examples/DBConnect.php:10) in /home/whatith/public_html/Examples/login.php on line 18
If you want to use the header function, you cant output anything, else headers will send automatically. so basically, you have to make sure you aren't echoing anything, and that's there isn't anything outside the php block being written.
Re: Redirection
Posted: Fri Jun 27, 2008 9:20 pm
by james3302
okay so how would I do this then? Call another page and only have head('Location: Main.html'); in the file and that's it. How do I call another page? Wouldn't that be redirection too?
Re: Redirection
Posted: Fri Jun 27, 2008 11:06 pm
by james3302
I just used java script to do it since PHP could not.
But now I have a control statement problem
When I enter the correct username and incorrect password it redirects me, but if I enter the incorrect username I do not get redirected. What's going on?
Code: Select all
if($username == $row['username'] && $password == $row['password']){
$query = "UPDATE users SET lastLogin = '$LastLogin' WHERE username = '$username'";
mysql_query($query) or die('Error, query failed');
mysql_close($conn);
echo "<center><h2>Welcome $username!</h2></center>";
}else{
?>
<script type="text/javascript">
<!--
window.location = "http://www.what-i-think.org/Examples/Main.html"
//-->
</script>
<?
}