doniyor
doniyor

Reputation: 37856

POST variable to another page while redirecting - php

I am redirecting the user back to login page if the login inputs arenot correct.

$sql = "select * from Driver where username=$username and pwd=$pwd";
$driver = mysql_query($sql);

if(!$driver){
  header("Location: http://domain.de/login.php");
  exit();
}

can i also pass message like "sorry, username isnot correct" to login page?

i dont want to use session. get isnot the option here

Upvotes: 0

Views: 284

Answers (5)

Zsolt Szilagyi
Zsolt Szilagyi

Reputation: 5006

For simply giving away a message, you can add it to the URL.

header("Location: http://domain.de/login.php?e=1234");

I recommend using error codes instead of full-length messages for better flexibility.

Note, how ever, that doing it right would require to implement a MVC pattern and then internally load the routing of the error page. But that might be too much for a small script.

I know you don´t ant feedback to your query. No need to worry, unless you are clueless about what SQL injection means.

Best regard

Zsolt

Upvotes: 1

gotardo
gotardo

Reputation: 71

I think the best solution is to load that login.php page as a part (the view) of the current script (the controller) and set a variable with the value of the message. Something like:

if(!$driver){
$message = "Sorry, username isnot correct";
} 
else {
$message = "Whatever";
}
include('login.php');

$message will be available for you inside login.php script.

Upvotes: 1

SAVAFA
SAVAFA

Reputation: 818

Change the query to:

$sql = "select * from `Driver` where `username`='$username' and `pwd`='$pwd'";

Note the backticks and single quotes

Upvotes: -3

Eugene
Eugene

Reputation: 1959

You may add get paramet to location header or save message flag in session. Like this:

 $sql = "select * from Driver where username=$username and pwd=$pwd";
 $driver = mysql_query($sql);

 if(!$driver){
     header("Location: http://domain.de/login.php?wasredirect=1");
     exit();
 }

 //////// In login.php

 if (isset($_GET['wasredirect'])) {
     echo "sorry, username isnot correct";
 }

Or this:

     $sql = "select * from Driver where username=$username and pwd=$pwd";
 $driver = mysql_query($sql);

 if(!$driver){
     header("Location: http://domain.de/login.php");
     if (!isset($_SESSION)) {
         session_start();
     }
     $_SESSION['redirect'] = true;
     exit();
 }

 //////// In login.php

 if (!isset($_SESSION)) {
     session_start();
 }
 $_SESSION['redirect'] = true;
 if (isset($_SESSION['redirect']) &&$_SESSION['redirect'] ) {
     echo "sorry, username isnot correct";
     unset($_SESSION['redirect']);
 }

Upvotes: 1

Nathan van der Werf
Nathan van der Werf

Reputation: 332

you could do it like

header("Location: http://domain.de/login.php?error=username");

and do on the other page

if ($_GET['error'] == 'username') {
    echo 'Sorry, username is not correct!';
}

EDIT: Watch out for SQL injection also

Upvotes: 1

Related Questions