Redirection[SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Redirection[SOLVED]

Post 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.
Last edited by james3302 on Mon Jul 07, 2008 7:18 am, edited 1 time in total.
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: Redirection

Post 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)
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: Redirection

Post 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.
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: Redirection

Post 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
evinowen
Forum Newbie
Posts: 1
Joined: Fri Jun 27, 2008 4:02 pm

Re: Redirection

Post 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.
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: Redirection

Post 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?
james3302
Forum Commoner
Posts: 53
Joined: Thu Aug 02, 2007 11:11 am

Re: Redirection

Post 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>
       <?
    }
 
Post Reply