php message passing with query string between pages - php

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.

Related

Php -- quick explanation how to use webpage.php?error=1

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

Header location to remain the same

I am currently creating a podcast website where I have a form for my users to enter a comment on the current episode they are listening to.
All my content is stored in mysql and data on my episode page is displayed like so
http://www.mysite.com/episode.php?id=1
With separate content for the episode.
On this post adding to database no repeat on refresh
I mentioned about the form resubmitting on refresh which is why I added
header('Location: index.php');
However. I would like this to visit the same page with a thank you message echoed.
I have tried
header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
echo "thank you";
But this brings back the error Parse error: syntax error, unexpected T_STRING
Please can someone advise me on the best way to do this? Thank you.
header('Location: episode.php?id=<?php echo $data['cast_id']; ?>');
you are using php tags in already started php tags.
you can simply do this ..
session_start(); // make sure session is up and running
// just before redirecting
$_SESSION['thankyou_msg'] = 1;
header('Location: episode.php?id='.$data['cast_id']);
now on index.php.
session_start();
if(isset($_SESSION['thankyou_msg'])){
echo "your thankyou message here";
unset($_SESSION['thankyou_msg']);
}
header() function is php function, so why you start php in it?
Also add exit() and store message in SESSION variable.
$_SESSION['msg'] = "thank you";
header("Location: episode.php?id=$data['cast_id']");
exit();

displaying a message after redirecting the user to another web page [duplicate]

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.

basic login user and session plus cookie

$query=sprintf("SELECT member FROM".$table_name."WHERE member=%s AND memberpwd=%s",$membername,$memberpassword);
$result=mysql_query($query);
if(!$result)
{
echo "Unable to select members";
mysql_close();
exit();
}
$rowNum=mysql_num_rows($result);
if(0!=$rowNum)
{
session_start();
$user=mysql_result($result,0,'member');
$_SESSION['user']=$user;
header('Location:index.php');
echo "Welcome ".$user;
exit();
}
I use that code to validate a login user and start his session but nothing is shown in the browser. I would also like the login user to appear in every page he visits, like a forums login member but I don't know how to use session with cookie to be stored somewhere so that he'll be remembered in each of his re-visits. This is the hardest part for me to do, could someone please help me ?
EDIT: Sorry, my knowledge of PHP just reaches and stops there-Session part. If I can have an ABC Session explained tutorial, that'll be really helpful, thank you.
The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT
(302) status code to the browser unless the 201 or a 3xx status code
has already been set. (header() on php.net)
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
It is not displayed because of redirect. Place echo "Welcome ".$_SESSION['user']; in index.php.
There is no $member defined, there at the bottom.
echo "Welcome ".$user;
... should work.

Session ID and variables in PHP

I have a login page where once a user is logged in successfully it echos a link to their personal page. When that page loads I want it to check if the user has access to it so someone doesn't try to just type in www.mywebsite.com/bob.php in the url. I tried to use a cookie to send the user info but I realized you can't use cookies after html has been written to the page. Does anyone know an efficient way to do this that is also fairly simple? Thanks
After the user logs in, assign his id to a session variable:
<?php
session_start();
$_SESSION["userid"] = $userid;
?>
On the protected page, check if the user has a $_SESSION["userid"] variable set:
<?php
session_start();
if (isset($_SESSION["userid"])) {
//show page
}else{
echo "No rights";
}
?>
It is true that you cannot set cookies when output has already been sent to the browser. A useful trick is to use output buffering. Basically, you begin your code with a call to ob_start() and end it with ob_end_flush(). Now you can set cookies (and any HTML header) wherever you want in your code.

Categories