Passing referrer to another page using JS or PHP - javascript

i am trying for this,
i have two pages : http://example.com/first-page and http://example.com/second-page
Here http://example.com/first-page is a simple landing page which has only a captcha to detect bots and
http://example.com/second-page is a main page.
So,when a visitor comes from a referrer to first page and the same referrer value should be passed to second page.
Is it possible with JS or PHP?

You can put the referrer value into the session array which can be accessed using PHP on any page, as below:
session_start();
$_SESSION['referrer_val'] = $_SERVER['HTTP_REFERER'];
On the second page, you can store the value in a local variable and then unset all global session variables and destroy the session in this manner:
$accessed_refer_val = $_SESSION['referrer_val'];
session_unset();
session_destroy();
These answers give more ways to accomplish this (like POST):
PHP Pass variable to next page
See also for more information: PHP Sessions | W3Schools

Related

Does history.pushState re-define variables on page?

I'm using history.pushState() in my wordpress site to changes the url/title of the page once a new post is dynamically loaded (AJAX). I want to store each post's id in an array, but for some reason the array variable gets re-defined.
At the top of my page I define the array variable:
<?php $array = array(); ?>
So when I push data to the array it only pushes to index 0.
Some help would be greatly appreciated.
pushState changes only the history stack.
It doesn't change any other variable accessible to JavaScript.
It couldn't possibly change any PHP variable because it runs on the client and does not trigger any HTTP request.
PHP is server side and is evaluated once and for all on each load. Hence why it is 0.
As #Quentin says in his answer: history.pushState is JavaScript (front-end). It has no connection with PHP (back-end) whatsoever.

Keep SESSION php after 3° pages refresh

i have a issue with my SESSIONS, on the first page the user clicks on a anchor, then if the user is logged in will be redirected without problem, but if not, first he will pass through login page, but when the user reach thelogin page, the variables are missing. (The login page occurs after the redirect page). So i have saved the SESSIONS with $_GET parameters.
How i can keep the current SESSIONS to redirect the user after the login?
Thanks!
EDITED
Page: retailer.php (this page is where its fired the parameters)
<a href="/go2store.php?rid=<?php echo $row['slug_title']; ?>&c=<?php echo $row_coupons['coupon_id']; ?>" onclick="javascript:window.location.href='<?php echo $row_coupons['logout_url']; ?>';" target="_blank">
Page: go2store.php (this page is where i'm saving the SESSION)
$_SESSION["myCupon"] = "/go2store.php?rid=".$_GET['rid']."&c=".$_GET['c'];
Page: redirect.php (This page its checking if the user is logged in, if yes, redirect to the URL on the SESSION, if not will send to login.php)
Page: login.php (here i have this on top of the code)
session_start();
$_SESSION["myCupon"] = "/go2store.php?rid=".$_POST['rid']."&c=".$_POST['c'];
But it comes empty, its not returning any values after the redirect.php page.
As noted by a user in the comments, you need to include:
session_start();
At the top of each page to carry session variables across pages, otherwise it won't recognise the vars you set initially.
We don't know how you're organising your code, but this needs to be present both before and after potential login.
You can easily set/get $_SESSION variables prior to login or indeed set/get upon login from other vars you might use in pages that aren't yet authenticated.

Can I send variables to the same JavaScript from two pages?

I have 2 pages where the user gives data that needs to be stored (the 1st is a string from the first page and the rest comes from a form from the second page). This webpage is not on a server, its on a laptop without any connection to the internet. The plan is for all that data to be written on to a text document.
My question is; can I send the first variable to a JavaScript like var = get.element... and then redirect the user to the second page where more variables get sent to the same JavaScript without the first disappearing. Because from what I have understood, all the scripts and variables "reset" when reloaded.
Extra, this is how I plan to write the info in a text file, does it look good or is there a better way to store data locally?
<?php
function createFile(){
$file = 'D:\test.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
Thanks for all your time!
I would think to pass some parameters from the first page using GET or POST request to the second page.
For example:
//yourApp/second_page.php?name=John Smith
is requested from the first page so you can get the data in the second page using $_GET['fname']
The solution #shadow proposed (using html5 local storage api) seems more clean and simple tho.

Is possible to get the previous form POST variable using javascript?

Client goes to example.com/form.html where a html POST form is displayed
Client fills the form with specific information and submit it to example.com/form.html
When example.com/form.html receives the POST request redirects the Client on example.com/redirected.html
Is possible to retrieve the variables that the client filled and POSTed to example.com/form using javascript ? The javaScript being deployed on example.com/redirected.html only . I presume that can be some "back" controls iframes and ajax involved but I couldn't find a reliable solution yet.
The operation will take place on the same domain so no cross domain issue is involved
Nope, I don't think this is possible.
You have to either
Store the posted value in a cookie
Store the posted value in a session variable on server side
Add the posted value as a GET parameter to the new URL
Use GET to post the original form, and painfully extract the posted value from document.referer on the new page
With HTML5, use localstorage. (The answer describes how to store object in localstorage- you could store an object of your form fields in there).
But you have to store the data on posting with js at example.com/form.html and then can get it on example.com/redirected.html. Without js at form.html, this is not possible with this method.
This works if you plan to use html5 and do not store too much data in it, iirc it has a limit of 5-10mb depending on the browser.
I don't think there is a way to do this by using plain html. With some server-side language (like PHP) it can be done with no problem.
I have been in a similar situation before, and the way I managed to give the data to JS is by including the data in a tag while preparing the output using PHP.
Assuming the redirected to php script receives the POST data from the script it's being redirected in. I would include this in the php code:
<?php
echo '<script type="text/javascript">';
echo 'var postData = '.json_encode($_POST).';';
echo '</script>'
?>
This will have the javascript know what the POST values contained.
To access the values from js:
//assuming you need the value for $_POST['foo']
var foo = postData.foo;
// or if json is encoded as just an associative array
var foo = postData['foo'];
If the POST data is not being passed to the redirected to script (haven't checked if this happens automatically), you could write the $_POST data in a $_SESSION variable, in the first php script:
<?php
$_SESSION['postdata']=$_POST;
?>
and then get it back from SESSION from the redirected to script.
<?php
$postdata = $_SESSION['postdata']; //use this to create the inline script in the html
unset($_SESSION['postdata']; //empty this from the SESSION variables
?>

edit $_SESSION with JS call

Hello
I have a script that uploads multiple images (this script is composed of many steps so I need to keep data through it). The upload is composed by a JQuery script that calls a PHP function to save images to a directory.
On this script I added this line
$_SESSION["login"]["auth"]["images"][] = $file_name;
that should add every image file name added to that array, but the $_SESSION variable remains untouched.
I can't change the $_SESSION from JS calls?
No, you can't modify PHP session variables from Javascript. However, a Javascript function can make an AJAX/XMLHttpRequest to a PHP script and that script in turn can modify the session. If the PHP script being requested by Javascript is not saving session data correctly you'll need to do some session troubleshooting in your PHP script:
Are you calling session_start() or is session.auto_start enabled in php.ini?
Are you using session_write_close() before redirecting away from or exiting the PHP script?
Check the headers (using a tool like Firebug). Is the PHPSESSID cookie being sent by the Javascript request?

Categories