Jump to content

[SOLVED] help with redirect link


chanfuterboy

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();

 

Link to comment
Share on other sites

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();

}

}

 

?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();
   }

}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.