I am on my page index.php. From here I go with a link to myform.php.
myform.php:
<?php
echo "My last visited page is:".$_SERVER['HTTP_REFERER'];
?>
<form action="success.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
The result I see on my page is:
My last visited page is http://www.mypage.com/index.php
Now I submit my form and come to success.php. Here I click my "back" button and come again to myform.php.
The result I see is now:
My last visited page is http://www.mypage.com/index.php
But what I expect here is:
My last visited page is http://www.mypage.com/success.php
You can use sessions..
Bottom of the success.php you can assign page name to sessions
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
When click on back button you can get that value using sessions(index.php),
echo $_SESSION['page'];
On that page at the bottom, you can assign current page name to session, then it can track on next page
The back button does strange things with variables and page refreshing. Try typing the http://www.mypage.com/myform.php address into the browser and seeing what happens.
N.B. I know this only deserves to be a comment, but reputation is a pain.
As you need to carry the previous info to forwarded page. Then it will be better if you use session. When you are visiting a page, store this current page in a session variable. Add this line at bottom on each page.
$_SESSION['page'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
And to echo
<?php
echo "My last visited page is:".$_SESSION['page'];
?>
So whole text will look like:
<?php
echo "My last visited page is:".$_SESSION['page'];
?>
<form action="success.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
<?php
$_SESSION['page'] ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
edit: Be careful when you are using multiple tab. Only latest info will be stored.
One way is, you can handle this by using PHP's _SERVER global, If the user first time visits your site, there will be no previous page, except that, the previous page will be last visited page identified by _SERVER, check out this script,
isset($_SESSION['current']) || $_SESSION['current'] = '';
// check for first visit to any page, initialize
if($_SERVER['SCRIPT_NAME'] != $_SESSION['current']){
// check if current page != previously recorded page
// order is important here in two lines, check it yourself
$_SESSION['previous'] = isset($_SESSION['current']) ? $_SESSION['current'] : '';
$_SESSION['current'] = $_SERVER['SCRIPT_NAME'];
}
You can make something like this, which ignores same page if user refreshes the page, disregarding the method being GET or POST.
You can include this script in common file to record the activity.
Array
(
[current] => /session/index.php
[previous] => /session/submit.php
)
Related
I have the following code on a web page that is entered by a number of unique URLs.
This web page cannot use history.back or other javascript as it is impossible to account for the amount of clicks on this page.
I came up with this solution below to try to get & save the original referer page URL for later use. Basically upon page entry if the SESSION Variable whodat1 is empty it should populate. The only problem is that session is overwritten each time a page refresh occurs.
How do I make this session permanent or not overwritten when the page refreshes?
<!-- back to map button-->
<?php
if(isset($_SERVER['HTTP_REFERER'])) {
if(empty($_SESSION["whodat1"]))
$_SESSION["whodat1"] = $_SERVER['HTTP_REFERER'];
//echo $_SESSION["whodat1"];
?>
<input type="button" value="Back To Maps" id="show" onClick="window.location.href='<?= $_SESSION["whodat1"] ?>'" style="height:30px; background-color:#006; color:white;">
<?php }?>
Refreshing won't make session disappear or expire. Make sure you have
session_start()
So, I was working on a PHP Application Center (Form), and I'm stuck at something.
I have 3 pages of form and 1 sumbit page. All are in .php extention. So, I want to take the <input name="name" type="text" placeholder="Name" required> from page one and:
<label>About yourself (Atleast 50 Words): <span>*</span></label><br />
<textarea name="yourself" placeholder="About yourself (Atleast 50 Words)" ></textarea> to the submit page.
I can take the values of Textarea & Textbox from page 3 to submit page through <?php echo $_POST['somethinghere']?>. But I can't take values from the First and second page.
Here are links to my php codes with style.css (pastebin.com):- http://pastebin.com/81vgHh5H
I had a look at the Pastebin and I think I can see the problem. You're setting the Session data on the same page that they're filling out (so you're assigning those fields to the session before they've even been filled in). You need to do that on page 2, not page 1. So your pattern is thus:
Show page 1 form.
User fills it out and hits submit.
Send user to page 2.
Page 2 grabs the information posted from Page 1, stores it in the session.
Page 2 shows the second form page.
User fills out page 2 and hits submit.
Send user to page 3.
Page 3 grabs the information posted from Page 2, stores it in the session.
And so on...
By the time you get to the last page, your session will contain all the information from the form and you can then process it however you need to.
Example:
Page 1:
<form action="page2.php">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
$_SESSION['page1text'] = $_POST['page1text'];
?>
<form action="page3.php">
<input type="text" name="page2text"/>
<input type="submit"/>
</form>
Page 3:
<?php
$_SESSION['page2text'] = $_POST['page2text'];
?>
<form action="page4.php">
<input type="text" name="page3text"/>
<input type="submit"/>
</form>
Page 4:
<?php
$_SESSION['page3text'] = $_POST['page3text'];
?>
<ul>
<li>Page 1: <?php echo $_SESSION['page1text'] ?></li>
<li>Page 2: <?php echo $_SESSION['page2text'] ?></li>
<li>Page 3: <?php echo $_SESSION['page3text'] ?></li>
</ul>
As you can see, page 4 winds up with access to all of the information from the previous pages.
Edit: This answer previously read: I would recommend having a model that contains all of the form's properties. As they fill out each page, populate the model with the data the user has entered and then when they successfully complete the form you can destroy the session data.
You can store them in session like, pass your data from page one to page two and in page two do following
$_SESSION['name'] = $_POST['name'];
Again, pass your data from page two to page three and in page three do following
$_SESSION['yourself'] = $_POST['yourself'];
This will set your name and yourself data in session and will be accessible from any page. Do not forget to include session_start() at the top of your page if that page uses session.
Now, you can access those session variable from submit page like,
echo $_SESSION['name'];
echo $_SESSION['yourself'];
I have a list of posts in my page, and I also have a form to search for posts.
This is my form:
<form action="" method="post">
<input type="text" name="search" onclick="if(this.value=='Search:')this.value=''" onblur="if(this.value=='')this.value='Search:'" value="Search:" />
<input type="submit" value="Search" name="sendForm" />
</form>
And then, when my form is submited with some values I store a session with my sql condition statment and I get only my list of news that have title like the value I wrote in my input.
And then, If I submit my form without any value I unset my session and I get my list of all news again.
if(isset($_POST['sendForm'])){
$search = $_POST['search'];
if(!empty($search) && $search != 'Search:'){
$_SESSION['where'] = "WHERE title LIKE '%$search%'";
header('Location: dashboard.php?exe=posts/index');
}
else{
unset($_SESSION['where']);
}
}
And this is working fine, but I need to click on my input search to unset my session and get all news again.
Even if I refresh my page Im getting only my news with title that I wrote in my input, and only if I click on my submit button I get again my list with all news.
Do you see how I can solve this, how I can unset my session not only clicking on my submit again but also with page refresh, becausee I want to show all news when I click in my link dashboard.php?exe=posts/index
Im trying with this code below on top of my page, but like this my search form is not working, is not returning any results.
if(!empty($_SESSION['where'])){
unset($_SESSION['where']);
}
Another quick alternative is instead of unset, you can set $_SESSION['where'] == "" then your query will not be affected as it does only contains nothing. Also please take note that your code is vulnerable to SQL injection attack.
You could store the user last visited page in session, and if it's the same than your current page and there is no submit, you know that it's a refresh.
Alternatively you could use the referer ($_SERVER['HTTP_REFERER']) although it's less reliable because it's client data and can be tampered.
So I am having a problem. I (probably) know what the problem is, but I dont know how to fix it.
I have a forum, with questions and comments. Now the username of the person who posted the question/ comment is a link to his/her profile. When I get to that link, I check wether the persons username is my username. If it is I can edit the profile with a button. Now when I click the button, I get errors and I'll explain why I think so. First i'll show you my code. This is on top of my profile page:
if(!empty($_GET['thread_username'])){
//If thread username link was clicked
$profileusername = $_GET['thread_username'];
}
else if(!empty($_GET['comment_username'])){
//If comment username was clicked
$profileusername = $_GET['comment_username'];
}
else{
}
$profileusername=$mysqli2->real_escape_string($profileusername);
The button on the profilepage reloads the page(and more) so that it goes through this code again, BUT the $_GET['thread_username']and the $_GET['comment_username'] are from the previous page. So when I reload it, these doesn't exist(So they are empty) and the page goes through the else. I have tested if it goes through the else by just giving the $profileusername a variable and it does. So when it goes through the else, the $profileusername isn't declared so I get all sort of errors. My problem is that when the page reloads, it shouldn't go through the else. It should just check the $_GET, but the page says it doesn't exist which I understand but how can I fix this?
My form now:
echo
<<<EOT
<form action="profile.php" method="post">
<input name="edit" type="submit" value="edit"></td>
<input type="hidden" name="thread_username" value="{$_REQUEST["thread_username"]}"/>
<input type="hidden" name="comment_username" value="{$_REQUEST["comment_username"]}" />
</form>
EOT;
Use $_REQUEST instead of $_GET, see the comments to the question.
I am new to php. How can I use session to remember my multiple form input like this:
In the first webpage, I can input for example a product name, when I click submit, the second page can tell me the product name I typed in. AND, if I go back to the first page again, and input another product name, and click submit, the second page can display the first product and the new product. If I input more product names continuously in this way, I can see all products listed in the second page.
How can I use session to do this?
First page (order.html):
<html><body>
<h4>Order Form</h4>
<form action="showorder.php" method="post">
Product name: <input name="prodname" type="text" />
<input type="submit" />
</form>
</body></html>
Second page (showorder.php):
<html><body>
<?php
$Pname = $_POST['prodname'];
echo "You ordered ". $Pname . ".<br />";
?>
</body></html>
You can use the $_SESSION array in the following fashion:
$_SESSION['products'][] = $_POST['prodname'];
That means that $_SESSION['products'] will be an array containing all the purchased products.
Don't forget to call session_start() in the beginning of all relevant pages, and filter your code against SQL injections and such.
session_start();
$_SESSION['everthing'] = $_POST['username'];
Then you can echo out the session, or just assign a variable to it. Sessions will stay there until the browser is closed