Jump to content

header redirection


PatrickLawler

Recommended Posts

I can NOT figure out why this code won't work. It seems to work fine in the tutorial I followed. I have the code copied the exact same way the tutorial said to. and ive googled my problem and can't figure it out... when I press the "Login!" button on my login.php file, the page does not redirect to admin.php... here is my code.... in my, "if else" statement, the else will echo text, but when i try to make the else redirect to a new session it won't work, it just loads a blank login.php page.... here is my code and the youtube tutorial I followed... http://www.youtube.com/watch?v=IPahfwPjEhI... If you can figure it out email [email protected] thank you so much!!!

 

login.php

 

<?php

mysql_connect("localhost", "FoleyHurley", "******");

mysql_select_db("blog1");

?>

<html>

<head>

<title>Login</title>

</head>

<body>

<?php

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

$name = $_POST['name'];

$pass = $_POST['password'];

$result = mysql_query("SELECT * FROM users WHERE name='$name' AND pass='$pass'");

$num = mysql_num_rows($result);

 

if($num == 0) {

echo "Bad login go <a href='login.php'>back</a>";

}else{

//I BELIEVE THE PROBLEM HAS SOMETHING TO DO WITH THE FOLLOWING 3 LINES... HELP!

session_start();

$_SESSION['name'] = $name;

header('Location: admin.php');

}

 

}

 

else{

?>

 

<form action='login.php' method='post'>

 

Username: <input type='text' name='name' /><br />

Password: <input type='password' name='password' /><br />

<input type='submit' name='submit' value='Login!' />

 

</form>

<?php

}

?>

</body>

</html>

login.php

Link to comment
https://forums.phpfreaks.com/topic/273306-header-redirection/
Share on other sites

You can't redirect once you've sent content to the output buffer. You've sent <html>... etc and then attempt to redirect after that so it'll never work.

 

Move the processing section above all html and use variables for errors. You can then test if the error variables are set and display the content wherever you want the errors to appear.

Link to comment
https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1406643
Share on other sites

You really should find yourself a new tutorial, as that one is using old, outdated and generally bad coding practises. Which is the direct cause of the problems you're getting, as said code will not work. Not to mention the complete lack of security.

 

Find yourself a proper tutorial, which uses MySQLi (or DBO), doesn't mix PHP and HTML code like above, uses input validation, and output escaping (real_escape_string () or Stored Procedures). You'll be doing yourself a huge favour, as you would not have to unlearn just about everything you learned afterwards.

Link to comment
https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1406660
Share on other sites

  • 4 weeks later...

first the session_start must be before any html tag.

And when you put this on server it would not redirect because the header must be before any output and if you want to use header then you need output buffering.

Try javascript

<script type="text/javascript">

window.location="xyz.php";

</script>

instead write this in place of header

Link to comment
https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1411893
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.