Is it possible in PHP to know which page a user came to a particular page from?
For example, the first page is index.php
and the second page is index2.php
.
Now the user goes to index.php
from index2.php
using a hyperlink which is on index2.php
.
Now I want to store the link in a database, for the page the user came to index.php
from, in this case index2.php
.
If it's on your own site you might as well use the session, HTTP_REFERER
is prone to spam or very likely simply not set, you can't trust it.
Maybe do something like:
<?php
session_start();
if (!isset($_SESSION['last_page'])) {
// first visit (landing)
} else {
// not first page
}
// insert into db
// set tracking for next page
$_SESSION['last_page'] = [
'page' => $_SERVER['REQUEST_URI'],
'time' => time() // know how long user was on the last page
];
Place it somewhere its hit on each page.
您可以使用$_SERVER['HTTP_REFERER']
访问最后一个页面URL(在本例中为原始URL)。
You can use the global variable $_SERVER
, it should tell you where the user came from. You can use it like this in your index.php:
echo $_SERVER["HTTP_REFERER"];
If you only want to track sites that are from you, like index.php
and index2.php
then you should use a session and set the value on each page, its more precise than the $_SERVER['HTTP_REFERER']
variable.
$_SERVER['HTTP_REFERER']
can easily be spoofed be the user.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.