0

I can't find this anywhere right now and looking for a quick answer.

What is the functionality of using webpagename.php?error=1?

Basically I want to use it so that when an error occurs logging in, I can redirect them to the page with a different message saying error.

I tried using

header('Location: loginpage.php');
echo '<p class="error">Error Logging In!</p>';

but nothing ever shows up. Thanks!

WestJackson
  • 1,067
  • 3
  • 11
  • 14

3 Answers3

1

in two way you can do this!

-1:

use like that parameter in URL

header('Location: loginpage.php?error=1');

in loginpage.php in JavaScript section check URL is contain error or not!

look at this link

just replace "q" with error!

make a function and check if contain error

show a error message!

<script>
function logincheck(){
 // code
}

if(logincheck()){
// do something
//alert('error');
// or append a error in html element
}
</script>

you can check this query string in php but JavaScript way is better

-2

use session

after validate set a session with error message like this:

$_SESSION['login_error'] = 'error in login!';

and in loginpage.php

check if that session have a value, show that to user,and unset session

like this:

if(isset($_SESSION['login_error'])){
   echo '<p class="error">Error Logging In!</p>';
   unset($_SESSION['login_error']);
}
Community
  • 1
  • 1
Pouya Darabi
  • 2,246
  • 18
  • 23
0

when u doing header, you already redirecting to the loginpage.php you should put the error message at loginpage.php.

or you can hold the redirection by doing something like

header('Refresh: 3;url=loginpage.php');

which redirect after 3 second;

onegun
  • 803
  • 1
  • 10
  • 27
0

On the page processing the login check, if login fails:

header('Location: loginpage.php?error=1');

Inside of loginpage.php place code where you want the message to appear:

<?php
If ($_GET['error']==1) {echo "login error!"} elseif ($_GET['error']==2) { echo some other message } .....
?>

The purpose of ?error=1 in the url is to pass a variable named ERROR with a value of 1 through the URL which is accessable to the redirection target page through $_GET

DMSJax
  • 1,709
  • 4
  • 22
  • 35