Jump to content


header redirection

header redirect location

  • Please log in to reply
3 replies to this topic

#1 PatrickLawler

  • Members
  • PipPip
  • Member
  • 10 posts
  • Age:23

Posted 18 January 2013 - 09:30 AM

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.c...=IPahfwPjEhI... If you can figure it out email patricktlawler@gmail.com thank you so much!!!


mysql_connect("localhost", "FoleyHurley", "******");
$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>";
$_SESSION['name'] = $name;
header('Location: admin.php');



<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!' />


Attached Files

#2 cpd

  • Members
  • PipPipPip
  • ¬_¬
  • 892 posts
  • LocationLondon, UK

Posted 18 January 2013 - 09:45 AM

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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#3 Christian F.

Christian F.
  • Staff Alumni
  • Advanced Member
  • 3,106 posts
  • LocationNorway

Posted 18 January 2013 - 11:51 AM

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.
Keeping it simple.

#4 alena1347

  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 12 February 2013 - 05:01 AM

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">
instead write this in place of header

Edited by alena1347, 12 February 2013 - 05:07 AM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users