Pass value from page to another in PHP

I am posting login status = failed, back to my login page. Here is my code -

header("location:index.php?login=fail");

      

but this is sending by url -

http://localhost/303/index.php?login=fail

      

Is there a way to pass the value without being displayed in the URL? And how to get this value on the second page?

+2


a source to share


3 answers


You are passing this value through a GET request, which is why it appears in the url. To pass a value without showing it in the url, you want to pass it through a POST request.

To do this, you do not want to "return" the value to your login page. Instead, any php form handles the user registration process after clicking the "Login" button, will determine what to show the user.

In PHP post, variables can be accessed by the global $ _POST object -

$_POST['username'];

      

Would get the value named "username" that you POST:

<form method="post" action="checkLogin.php">
Username:
<input type="text" name="username" maxlength="25" />
Password: 
</td><td><input type="password" name="password" />
<input type="submit" name="submit" value="Login">
</form>

      

To dynamically save and show errors to the user, you can save them in the session, for example have a file called "errors.php"



<?php
if (isset($_SESSION['errors']))
{
    echo $_SESSION['errors'];
}

unset($_SESSION['errors'])
?>

      

And in your php that checks the login do:

session_start();
$_SESSION['errors'] = "Invalid username or password.";

      

Then redirect to the login page (don't pass any variables) and your form always has this field:

<?php include("errors.php"); ?>

      

If you didn't have any errors, it won't display anything and the login page will look fine.

Note . In whatever php form you use session_start (), it must be the first one in the form.

+1


a source


Other ways to use session or hidden fields , but you what you do are fine for this purpose. You can later get a value like this:



if ($_GET['login'] === 'fail')
{
  // failed.......
}

      

0


a source


there are several ways to accomplish your task.

  • The modern way of AJAX. The form is submitted using AJAX. Don't reload the page until the password is correct. Errors are shown in situ. Requres javascript.
  • Post / Redirect / Get . The form is submitted using regular POST. No redirection to errors shown in-place.
  • when we store the error in the session
0


a source







All Articles