tags:

views:

145

answers:

3

I am sending login status = fail, back to my login page.Here is my code-

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

but that is sending through URL like-

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

is there any way to pass value without showing in URL? And how to get this value on the second page?

A: 

Other ways are to use session or hidden fields but you what you are doing is fine for the purpose. You can later retrieve the value like this:

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

You are passing that value via a GET request, which is why it appears in the URL. In order to pass a value without showing it in the URL, you want to pass it via a POST request.

In order to do this you aren't going to want to "return" the value to your login page. Instead, whatever php form is handling the process of logging in the user after they click the "login" button, will decide what to show the user.

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

$_POST['username'];

Would get the value with the name "username" that you passed via 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>

In order to dynamically save and show errors to the user, you can store 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 your login page (don't pass any variables) and on your form always have this field:

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

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

Note: In any php form that you use a session_start(), it HAS TO BE THE FIRST THING in the form.

Zachary
he asks for redirect
Col. Shrapnel
but in header(--) how can I specify method="Post" ??
nectar
You wouldn't specify anything in the header - if you redirect them back to the login page or "index" or whatever - have the <?php include("errors.php")?> on that page. Then whatever errors you stored in the session will show up there. The values in the session are stored on the server, you don't need to pass them.
Zachary
not the "first thing" and not "in the form" but merely before any output
Col. Shrapnel
this is my code of checklogin.php can you please tell me how to send back login=fail value ?$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"session_register("myusername");session_register("mypassword"); $_SESSION['loggeduser'] = $myusername;header("location:LoggedUser.php");}else { header("location:index.php?login=fail"); }ob_end_flush();
nectar
Again, you don't want to send back a "fail" value. You just want to do else { $_SESSION["errors"] = "Login failed";header("location:index.php");}Then on your index.php, have somewhere <?php include("errors.php")?>. errors.php should contain the code I mentioned above.
Zachary
Oh sorry I didn't refresh my page so I asked again....In errors.php do I need to write session_start(); ??
nectar
Yeah, you probably should. As an FYI, it doesn't hurt to have more than one session_start() happen.
Zachary
> I have created errors.php as mentioned above.> In checklogin.php, I am setting the $_session["error"]="Invalid Login Id or Password";> In index.php I have included <?php include("errors.php"); ?>now I am lilbit confused weather I have to check If($_session['errors'] == "Check Login name....") or it will automatically takes care of? why we are including errors.php in index.php file???
nectar
You have to include it wherever you want error messages to show up at. It is what is printing the error messages that you are saving. If you don't put anything in it, it won't print anything, and it unsets the errors after it prints them.
Zachary
It is not showing thats why I asked...what I have to write to show up the error message stored in session??
nectar
A: 

there are several ways to accomplish your task

  • Modern AJAX way. Form being sent using AJAX. No page reload until password is correct. Errors shown in place. Requres javascript.
  • Post/Redirect/Get pattern. Form being sent using regular POST. No redirect on errors, shown in place.
  • sessions, when we store an error in the session
Col. Shrapnel