Jump to content


Photo

header redirection

header redirect location

  • Please log in to reply
3 replies to this topic

#1 PatrickLawler

PatrickLawler

    Member

  • Members
  • PipPip
  • 10 posts
  • Age:23

Posted 18 January 2013 - 04: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!!!

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>

Attached Files



#2 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 18 January 2013 - 04: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.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 18 January 2013 - 06: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

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 12 February 2013 - 12: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">
window.location="xyz.php";
</script>
instead write this in place of header

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com