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 patricktlawler@gmail.com 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
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
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
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

Edited by alena1347
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.