antwonw Posted June 25, 2009 Share Posted June 25, 2009 Hey, So I have implemented a login script to my site. The only problem is when it loads it refreshes the page and displays "Welcome $username! You are logged in! Visit the Galleries Page!" I tried using a "header('Location: /galleries/');" to redirect on login but I couldn't get it to work. Here is my login page login.php <?php require_once('common.php'); $error = '0'; if (isset($_POST['submitBtn'])){ // Get user input $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; // Try to login the user $error = loginUser($username,$password); } ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Galleries</title> <link rel="stylesheet" type="text/css" media="screen" href="css/login.css" /> </head> <body bgcolor="000000"> <table id="rotator" border="0" width="100%" height="100%"> <tr> <td align="center"> <table id="loginbox" border="0" width="339" height="339"> <tr> <td> <table border="0" background="" width="280" height="280" align="center"> <form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td colspan="2" height="40px"> </td> </tr> <tr> <td align="center" colspan="2"> <h2>Login</h2> </td> </tr> <tr height="5px"> <td align="center"> <font color="#ffffff"> Username: </font> </td> <td> <input class="text" type="text" name="username" size="18" maxlength="18"> </td> </tr> <tr height="5px"> <td align="center"> <font color="#ffffff"> Password: </font> </td> <td> <input class="text" type="password" name="password" size="18" maxlength="18"> </td> </tr> <tr height="60px"> <td align="center" colspan="2"> <input class="text" type="submit" name="submitBtn" value="Login"> <input class="text" type="reset" name="Clear" value="Clear"> </td> </tr> <tr> <td align="center" colspan="2" height="50px"> <?php if (isset($_POST['submitBtn'])){ ?> <?php if ($error == '') { echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>'; } else echo $error; ?> <?php } ?> </td> </tr> </form> </table> </td> </tr> </table> </td> </tr> </table> </body> </html> Here is the common.php file common.php <?php session_start(); function registerUser($user,$pass1,$pass2){ $errorText = ''; // Check passwords if ($pass1 != $pass2) $errorText = "Passwords are not identical!"; elseif (strlen($pass1) < 6) $errorText = "Password is to short!"; // Check user existance $pfile = fopen("pwd.asp","a+"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(':', $line); if ($tmp[0] == $user) { $errorText = "The selected user name is taken!"; break; } } // If everything is OK -> store user data if ($errorText == ''){ // Secure password string $userpass = md5($pass1); fwrite($pfile, "\r\n$user:$userpass"); } fclose($pfile); return $errorText; } function loginUser($user,$pass){ $errorText = ''; $validUser = false; // Check user existance $pfile = fopen("pwd.asp","r"); rewind($pfile); while (!feof($pfile)) { $line = fgets($pfile); $tmp = explode(':', $line); if ($tmp[0] == $user) { // User exists, check password if (trim($tmp[1]) == trim(md5($pass))){ $validUser= true; $_SESSION['userName'] = $user; } break; } } fclose($pfile); if ($validUser != true) $errorText = "<font color='red'>Invalid username or password!</font>"; if ($validUser == true) $_SESSION['validUser'] = true; else $_SESSION['validUser'] = false; return $errorText; } function logoutUser(){ unset($_SESSION['validUser']); unset($_SESSION['userName']); } function checkUser(){ if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){ header('Location: ../login.php'); } } ?> I still want it to display "Invalid username or password!" and the other error's on the login.php page. But I want a successful login to redirect to "galleries.php" Can anyone help me with this. I can't seem figure this one out. Thanks! AW Quote Link to comment Share on other sites More sharing options...
ldougherty Posted June 26, 2009 Share Posted June 26, 2009 In your login.php replace this <?php if (isset($_POST['submitBtn'])){ ?> <?php if ($error == '') { echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>'; } else echo $error; ?> <?php } ?> with this <?php if (isset($_POST['submitBtn'])) { if ($error == '') { echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; sleep(3); //Pause for 3 Seconds header("Location: ./galleries/"); //Redirect to Galleries } else { echo $error; } } ?> Quote Link to comment Share on other sites More sharing options...
antwonw Posted June 30, 2009 Author Share Posted June 30, 2009 In your login.php replace this <?php if (isset($_POST['submitBtn'])){ ?> <?php if ($error == '') { echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; echo '<h3><a href="/galleries/">Visit the Galleries Page!</a></h3>'; } else echo $error; ?> <?php } ?> with this <?php if (isset($_POST['submitBtn'])) { if ($error == '') { echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; sleep(3); //Pause for 3 Seconds header("Location: ./galleries/"); //Redirect to Galleries } else { echo $error; } } ?> This did not work. I got a return error of the follow: Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\site.org\httpdocs\login.php:28) in C:\Inetpub\vhosts\site.org\httpdocs\login.php on line 77 Any suggestions? Thanks! AW Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 2, 2009 Author Share Posted July 2, 2009 Can anyone help me? It'd be very much appreciated! Quote Link to comment Share on other sites More sharing options...
AwptiK Posted July 2, 2009 Share Posted July 2, 2009 <?php if (isset($_POST['submitBtn'])) { if ($error == '') { ob_start(); echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; sleep(3); //Pause for 3 Seconds header("Location: ./galleries/"); //Redirect to Galleries ob_flush(); } else { echo $error; } } ?> Output buffering, because you can't use header() after any output has been sent. Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 2, 2009 Author Share Posted July 2, 2009 <?php if (isset($_POST['submitBtn'])) { if ($error == '') { ob_start(); echo "<h3>Welcome $username! <br/>You are logged in!<br/><br/></h3"; sleep(3); //Pause for 3 Seconds header("Location: ./galleries/"); //Redirect to Galleries ob_flush(); } else { echo $error; } } ?> Output buffering, because you can't use header() after any output has been sent. I still got the same thing! Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\site.org\httpdocs\login.php:28) in C:\Inetpub\vhosts\site.org\httpdocs\login.php on line 78 Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 4, 2009 Author Share Posted July 4, 2009 So does anyone have a suggestion for another PHP Authentication Application that is purely PHP. I can't do SQL/Databases at all or else I would. Any ideas or suggestions? I also need it to redirect to a different page once login. Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted July 4, 2009 Share Posted July 4, 2009 The rules are simple. You cannot use the header function once output has been sent to the browser. Organise your code in such a way as to ensure that and you'll be fine. Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 5, 2009 Author Share Posted July 5, 2009 The rules are simple. You cannot use the header function once output has been sent to the browser. Organise your code in such a way as to ensure that and you'll be fine. Problem is I can read and understand PHP to a limit but writing it is another thing for me. I'm self taught and still learning. Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 6, 2009 Author Share Posted July 6, 2009 So I take it no one can help me out or at least point me in the right direction? I can't use MYSQL unfortunately. Quote Link to comment Share on other sites More sharing options...
trq Posted July 7, 2009 Share Posted July 7, 2009 Problem is I can read and understand PHP to a limit but writing it is another thing for me. I'm self taught and still learning. Most everyone is self taught in regard to php, and great way to learn is to fix problematic code. So I take it no one can help me out or at least point me in the right direction? You've been given a push in the right direction. Without writing the code for you there isn't much more we can do. Quote Link to comment Share on other sites More sharing options...
jokerbla Posted July 7, 2009 Share Posted July 7, 2009 Instead of using the PHP header() you can try another method using JavaScript $redirectUrl = "http://localhost/www/target-redirect.php"; print "<script type=\"text/javascript\">"; print "window.location.href = '$redirectUrl'"; print "</script>"; Had the same problem some days ago. Quote Link to comment Share on other sites More sharing options...
antwonw Posted July 15, 2009 Author Share Posted July 15, 2009 AWESOME!!! Thanks for the help!!! Here is what I did. if ($error == '') { $redirectUrl = "/galleries"; print "<script type=\"text/javascript\">"; print "window.location.href = '$redirectUrl'"; print "</script>"; } else echo $error; Again, thanks for the help!!! Quote Link to comment 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.