PatrickLawler Posted January 18, 2013 Share Posted January 18, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273306-header-redirection/ Share on other sites More sharing options...
cpd Posted January 18, 2013 Share Posted January 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1406643 Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1406660 Share on other sites More sharing options...
alena1347 Posted February 12, 2013 Share Posted February 12, 2013 (edited) 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 February 12, 2013 by alena1347 Quote Link to comment https://forums.phpfreaks.com/topic/273306-header-redirection/#findComment-1411893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.