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!
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']);
}
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;
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
Related
As a part of a very simple login script, I create a session variable (or at least that is what I think it's called) with the username, and use that as a check of whether or not a user is logged in. The variable is set in the login script, using the following code:
if($count==1) {
session_start();
$_SESSION['ed_user'] = $ed_user;
header("location:main.php");
} else {
echo "Incorrect user or password, please try again.";
}
I know that the first part of this if-statement is run, since I am not presented with the error message. On the page it directs to (main.php) the first lines of code should check if $_SESSION['ed_user'] is set, and return to index.php, if this is not the case. This is done with the code:
if(!$_SESSION['ed_user']){
header("location:index.php");
}
However, it seems to always return me to index.php after login. I have tried to check if the variable exists, using the following line:
<p><?php echo"Current user: ".$_SESSION['ed_user'];?></p>
Which indicates that the variable is empty. What am I doing wrong here?
You need to call
session_start();
On every page.
On your main.php file...
session_start();
if(!isset($_SESSION['ed_user'])){
header("location:index.php");
}
You need to call session_start() to access session variables.
In PHP after logout send a query string from logout.php in header like header("location:login.php?call=logout"); and on login page i got that call query string variable to show message on login page that you are logged out or etc.
But if i will directly go to login.php?call=logout link then same you are logout messgage will appear. but no logout process followed at this time.
How could i get rid from this problem.
if direct url login.php?call=logout passed then no log out message should displayed.
logout.php
<?php
header("location:login.php?call=logout");
?>
Login.php
<?php
if($_GET['call']=='logout'){ echo "you are logged out.";}
?>
Put your messsage to session. Example:
$_SESSION['message'] = 'Some message';
header("location: login.php");
exit;
// login.php
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
Is that actually a problem worth solving? It's the easiest mechanism to display a message and unless you link to login.php?call=logout from anywhere else, nobody should ever get into this situation to begin with.
To "verify" that the message should actually be displayed you need to keep state server-side. Typically you'd use a session (even without logged in user) to store the information that a message should be displayed on that page.
I have a page, q.php, that is a user submitted post defined by its id, (for example, q.php?id=1 would be a certain post that uses the $id variable to pull all the rest of the information from the database).
I am trying to code a comment submission on the same page, and account for the fact that a user might not enter anything into the field. If this happens, I want the page (e.g. q.php?id=1) to load again with an error message.
I can do the error message with an empty variable that is then given a value by the php file that the form activates. However, I am having a problem navigating back to the specific post. I tried to use include('q.php?id=$id) where $id is set to a number, but I understand that this is not its purpose and it does not accept variables
What should I be using instead?
EDIT
answer.php (file that the form activates):
require 'q.php';
$_GET['id'] = $id;
$_SESSION['error'] = "Please fill in all of the fields.";
q.php:
if ($_SESSION['error'] !== 0) {
echo "<p style='color: #AA1111;'>".$_SESSION['error']."</a>";
unset($_SESSION['error']); // this isn't happening...
}
If you really must include the page inline, you can always modify $_GET:
$_GET['id'] = $id;
require 'q.php';
But an error message sounds like it could be accomplished with a session variable or a redirect. A redirect could look something like:
header('Location: q.php?id=' . $id . '&error=The+error');
exit();
Then, you check for $_GET['error'] in q.php. Using a session variable for that would be much the same, except instead of adding error as a querystring parameter, you use $_SESSION['error'] and unset it immediately.
You could use header("Location: q.php?id=$id"); exit;, but you would need some other way to send the error message (example: save it in $_SESSION)
You might be able to set the $_GET array how you want it - in this case, $_GET = Array("id"=>$id); then include("q.php"). This is generally considered haxy, though, and may result in problems if you don't use include_once properly.
This question already has answers here:
Redirect to another page with a message
(6 answers)
Closed 8 months ago.
I am doing HTML with PHP and MySql.
After some database operation that was performed by the user, my system redirects the user to the original database page in order to show him the updated table. (I am done with this part).
At the same time, I wish to display a message to the user on the original page (the one where the system moved) to notify him with the success of the operation. How can I possibly display this message?
Here's my php code that moves to the other page.
Header( 'Location: Database.php');
Header( 'Location: Database.php?success=1' );
And in the Database.php page :
if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
// treat the succes case ex:
echo "Success";
}
Store it in the session as sort of a "flash"-message:
$_SESSION['message'] = 'success';
and show it in Database.php after the redirect. Also delete its content after displaying it:
print $_SESSION['message'];
$_SESSION['message'] = null;
The advantage of this is, that the message won't be shown again every time the user refreshes the page.
you can do this:
$_SESSION['msg']="Updation successfully completed";
header("location:database.php");
on database.php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
One solution is to put the message in a SESSION, in your php file. So in the original page, you get that SESSION variable, and display that.
ex:
in your php file:
session_start();
$_SESSION["message"]="MESSAGE OF SUCCESS"
In you original file:
session_start();
if(isset($_SESSION["message"]))
{
echo"SUCCESS OR THE MESSAGE SET IN THE VAR SESSION";
unset($_SESSION["message"]);
}
The best way to solve this problem is set a session message after the success of your operation in the process page.
Then in the redirected page check whether the session message is set or not.If it is set then simply echo that message.
The below code may help you.
$_SESSION['MSG']="Your data is saved";
Header( 'Location: Database.php');
exit;
//now in the database.php page write at the top
<?php
if(isset($_SESSION['MSG'])){
echo $_SESSION['MSG'];
}
?>//its very simple,you can also format the message by using different html attributes
Before redirecting to new page, you can set a cookie with the message you want to show, upon loading the original page you will see if this special cookie is set and if it is you can display the success message stored in the cookie.
When using this code to try an show an error, the page redirects to myaccount, but should only redirect there if the required field has been completed, for example;
error message appears if birth_country is not chosen
if(empty($birth_country))
{
$err[] = "ERROR - Enter Birth Country";
header("Location: personal.php?msg=$err[0]");
}
but if it is chosen, should redirect to...
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
However it always goes to myaccount no matter what
Thanks
After your first header put exit as well. Your code just continues on and it likely emits the second location header as well, negating the first.
Like Frits says, you will have to print out the error to the screen if you want it to be seen; however, you can not print out any HTML before a header or the header won't work. If you want to print the error on the redirecting page then I would suggest putting the error message in a $_SESSION cookie, then call and echo that cookie/variable on the redirecting page. But by doing this, you will need to use php session_start() at the top of the question page and the top of your redirect page in order to use the $_SESSION variables
Also, your second header
header("location: myaccount.php?id=" . $_SESSION['user_id']. "");
exit();
Should look like this:
header("Location: myaccount.php?id=$_SESSION['user_id']")
By using the double quotes, you don't need to add the $_SESSION[] tag, then add blank "" at the end.