chanfuterboy Posted August 6, 2009 Share Posted August 6, 2009 hi, if login in my code you see $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in."; exit(); how can i put a redirectlink after echo succesfully login, it goes to another page after 2 sec so? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Hi chanfuterboy, Normally you would use the header function as below: header('Location: anotherpage.php'); But the header function needs to be called before any output is sent so in your case this will not work as you want to echo a successful login message. So I would use something like: echo '<META HTTP-EQUIV="Refresh" Content="2; URL=anotherpage.php">'; This will redirect to "anotherpage.php" after "2" seconds. If you wanted to use the header function you could probably achieve it by using ob_start() and ob_flush() but for ease of use I would just go with the above. The final code would look like: $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=anotherpage.php">'; exit(); Hope this helps. Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 hi, it help perfectly, now the problem comes, that when i go back to login page( beeing login), i dont wanna see the login fields, instead of the redirect page. can you helpme Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Hi chanfuterboy, Not sure I understand. Are you saying that when you login to your site, you get the login message then get redirected. But, now you're logged in you want to return back to the login page but not see the login fields? Why would you want to return back to the login page after logging in? Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 Thanks it works now I want to do one more thing. Bad login, echo could not login, so i pin it to go to login fields back. ( the script is on the same page of login.php). As you login good, i put to redirect to test.html. But when i do it, it redirect in login.php also why? or die ("Error - Couldn't login user."); echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (!empty($row[username])) // he got it. { $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 the post 4 of yours let it so,read post 5 Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Hi chanfuterboy, Sounds like a problem with an if statement somewhere, could you post the full code please? Thanks Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 Full code as requested <?php session_start(); // Check if he wants to login: if (!empty($_POST[username])) { require_once("connect.php"); // Check if he has the right info. $query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die ("Error - Couldn't login user."); echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (!empty($row[username])) // he got it. { $_SESSION[username] = $row[username]; echo "Welcome $_POST[username]! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; exit(); } } ?> Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Your login code needs some tweaking, I would try something like: <?php // we must never forget to start the session session_start(); if (isset($_POST['Username']) && isset($_POST['Password'])) { $username = $_POST['Username']; $password = $_POST['Password']; // check if the user id and password combination exist in database $query = "SELECT * FROM members WHERE username = '$username' AND password = PASSWORD('$password')"; or die ("Error - Couldn't login user."); $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); } else else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; exit(); } } ?> In your original code you place the <META HTTP-EQUIV="Refresh" Content="1; URL=login.php"> line in the middle of the if statement so it was always going to run. Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 MISSING something the script does not work Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Sorry, I made a typo - look for: } else else // bad info. and change to: } else // bad info. Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 the require of connection to db is needed also, it still not working Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 im thinking there is a mest up in the lines // check if the user id and password combination exist in database $query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die ("Error - Couldn't login user."); and the of yours with the rest scripts Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Try: <?php // we must never forget to start the session session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; require_once("connect.php"); // check if the user id and password combination exist in database $query = "SELECT * FROM members WHERE username = '$username' AND password = '$password'"; or die ("Error - Couldn't login user."); $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; exit(); } } ?> Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 nop, the page become blank as before. something is not good Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Any good? <?php // we must never forget to start the session session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; require_once("connect.php"); // check if the user id and password combination exist in database $query = "SELECT username FROM members WHERE username = '$username' AND password = '$password'"; $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; exit(); } } ?> Othwerwise post your full code including your HTML. Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 now it show the fields, but as login good or not, it show Error - Couldn't login user. and it does not redirect also <?php // we must never forget to start the session session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $username = $_POST['username']; $password = $_POST['password']; require_once("connect.php"); // check if the user id and password combination exist in database $query = "SELECT username FROM members WHERE username = '$username' AND password = '$password'"; $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); if (mysql_num_rows($row) == 1) { // the user id and password match, // set the session $_SESSION[username] = $row[username]; // after login we move to the main page echo "Welcome $username! You've been successfully logged in."; echo '<META HTTP-EQUIV="Refresh" Content="2; URL=test.html">'; exit(); } else // bad info. { echo "Error - Couldn't login user.<br /><br /> Please try again."; echo '<META HTTP-EQUIV="Refresh" Content="1; URL=login.php">'; exit(); } } ?> <html> <head> <title>Login</title> </head> <body> <form action="login.php" method="post"> <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1"> <tr> <td width="100%"><h5>Login</h5></td> </tr> <tr> <td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[username]; ?>"></label></td> </tr> <tr> <td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></td> </tr> <tr> <td width="100%"><input type="submit" value="Login!"></td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 nothing yet? Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 See below. Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Change: $row = mysql_fetch_array($query) or die ("Error - Couldn't login user."); to: $row = mysql_query($query) or die ("Error - Couldn't login user."); Quote Link to comment Share on other sites More sharing options...
chanfuterboy Posted August 6, 2009 Author Share Posted August 6, 2009 hi, i just got that figur out that there was the problem.thanks it is solve, i just put also a load picture before redirect and work fine now bye Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 6, 2009 Share Posted August 6, 2009 Glad it's now working. If you scroll down near the bottom of this page you will see the "Topic Solved" link. If you click there it will mark this topic as solved. Thanks. 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.