PHP Redirect with Post Data
Hi,
I am a newbie PHP programmer and trying to code a small blog.
I will explain what I am trying to do.
page1.php: Has a table of all posts in the blog
page2.php: This page has a form where you can add a Post
Page 2 posts to itself and then processes the data, if it successful then uses header() to redirect back to page1 which shows the table.
Now what I want to do is to be able to have a small message on page 1 above the table saying your blog post has been successfully submitted but I’m not sure how I can pass data back to page 1 after the form processing.
Set it as a $_SESSION value.
in page2:
$_SESSION['message'] = "Post successfully posted.";
in page1:
if(isset($_SESSION['message'])){
echo $_SESSION['message']; // display the message
unset($_SESSION['message']); // clear the value so that it doesn't display again
}
Make sure you have session_start() at the top of both scripts.
EDIT: Missed ) in if(isset($_SESSION['message']){
You could also just append a variable to the header location and then call it from the page.
header('Location: http://example.com?message=Success');
Then wherever you want the message to appear, just do:
if (isset($_GET['message'])) {
echo $_GET['message'];
}
A classic way to solve this problem is with cookies or sessions; PHP has a built-in session library that assists with the creation and management of sessions:
http://www.php.net/manual/en/book.session.php
Here is a concise example:
Page 1
session_start();
if (isset($_SESSION['message'])) {
echo '<div>' . $_SESSION['message'] . '</div>';
unset($_SESSION['message']);
}
Page 2
session_start();
// Process POST data
$_SESSION['message'] = 'Hello World';
// Redirect to Page 1
Related
I have an assignment whereby I provide an html login form on a page called index.php and upon entering the correct username and password you will be redirected to the same index.php page but now the login form will disappear and be replaced with html links to three other php pages.
In other words the html links are not visible before successfully logging only after. All of this happens on the same php page (index.php). Any suggestions would be greatly appreciated.
First of all a brief knowledge to session variable.
1. You can Set the Session using $_SESSION['username']="your-username-from-html-post";
2. This is a global variable i.e. it will be available for use to all files throughout your website.
3. To delete session you can use unset($_SESSION['username']);
4. Session are very useful to make a login form in PHP and HTML as they provide a universal format to check if the user is logged in or not. By the Way- the session variable will be for your website only and for the same computer only on which user has logged in not for other computers(obviously)
Now your Answer
You can write in your index.php like this
If (isset($_SESSION['your-session-variable'])) {
echo 'YOUR LINK 1';
echo 'YOUR LINK 2';
echo 'YOUR LINK 3';
} else {
//insert your form code here
}
Replace $_SESSION['your-session-variable'] with your own Session Name
What this does is It check is the $_SESSION variable is set by isset
If answer is true it will print your links else it will print your html code!
I have 3 pages.
-page1.php sends data through a form with post action
-page2.php recieves data from page1.php. In page2.php there is a link to page3.php
-page3.php does some staff.
When i click to go back from page3 to page2 occures
Confirm Form Resubmission
ERR_CACHE_MISS
I understand the reason of this behaviour. When I take data from page1 to page2 I store some of them in session so i don't need always to use submit. How can i check if submit was done in order to continue normally if no submit was used?
Here is some code of page2:
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
global $dbcnx;
if (isset($_POST['submit']))//if i submit i store in session
{
$_SESSION['team_name'] = $_POST['teams'];
echo $_SESSION['team_name'];
}
else //do nothing
{}
//conitnue code normally
.
.
.
That was my last try. Any response will be helpfull. Thanks in advance!
The usual way to prevent that is to redirect to a page with no post data, click the link to see more info on How to make a redirect in PHP?
The answer is that you should redirect to a specific page using php.that is done with header().
this should be a help:
header("Location: http://www.something.com/any/thing.php") //the page can be anything
Of course, if you're using xampp or wamp or whatever server, use it this way:
header("Location: http://localhost/any/thing.php") //the page can be anything
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.
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.
1: i use register.php to sign up the clients,
2: the data collected from the form is send to 1.php, it is saved in database
3: after form data is saved in database, 1.php forwards selected form data (myValue) to register.php?myValue='abc'
in 1.php, i am saving a session variable like this
#session_start();
$_SESSION['color']='blue';
the code of register.php is
if (isset($_SESSION['color'])) {
header('Location: http://mydomain.com/thankyou.php');
}
else {
#session_start();
some other stuff that was initially use for signing up the clients
my logic is to check for session variable and to redirect it to some-other page
when step 1 , step 2 and step 3 are complete, page should be
redirected to thankyou.php
currently, when step 1, step 2, step 3 are done, instead of opening thankyou.php, the following page is being opened
http://mydomain.com/register.php?myValue='abc'
however, if i re-open register.php or go back to step one (opening register.php), thankyou.php is displayed...
can somebody guide me where i am doing the blunder? why redirection is not being successful although session variables are being created?
code Update
i tried the following code at the top of my register.php
#session_start();
if (isset($_SESSION['color'])) {
header('Location:http://mydomain.com/thankyou.php');
exit;
}
else{
remaining stuff
it occasionally do the trick, redirects to the page, while on occasion (greater in number), it fails in redirecting to thankyou.php,, also the code needs to delete complete history and cache to work (after doing so, still miss hits occurs..)
Make sure you use exit(0); right after you do a header redirect otherwise php will still parse and run the rest of your script, sometimes it can cause some funny behaviour.
In your register.php, you can't test for the session variable before you issue the session_start, so your code should be more like:
session_start();
if (isset($_SESSION['color'])) {
header('Location: http://mydomain.com/thankyou.php');
}
else {
// Something else....
EDIT:
Another thing I've found useful when trying to set session variable in conjunction with redirects is to proceed to the redirect only after running a function. Here's how it would work:
$throwAwayVariable = setColor('blue');
if($throwAwayVariable ){ // separated out into a function so it wouldn't redirect before the session variable was saved
session_write_close();
header("Location: http://mydomain.com/thankyou.php");
}
function setColor($color){
#session_start();
$_SESSION['color']='blue';
return true;
}
Since not all your code is posted, you'll have to figure out where this goes, but I've always had my session vars work after this process.
Your session_start() call in register.php needs to be BEFORE you call any $_SESSION variables.
I have the same issue, then I try to add session_start and session_write_close, and it works!
session_start();
$_SESSION['status'] = 'Updated Poem successfully';
session_write_close();
header("location: index.php");