Is there anyway to determine if a page has come from a php redirect?
I have a session saved of the last time the page was refreshed, but i dont want it to set if the page has come from an instantly referred one.
To give some idea of what i'm doing, i have this code on my site to stop users refreshing more than once per second:
$now = time();
if ($_SESSION['click'] > ($now-1)) {
exit("Woah, you're clicking too fast!") ;
}
$_SESSION['click'] = $now;
However, I don't want the click session to set if the page has from from a php redirect, or a form post, etc. Any solutions?
How about using referer?
if( $_SERVER['HTTP_REFERER'] == "http://mydomain.com/formmail.php" )
Related
How can I detect if user refreshed the page with PHP?
I tried $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
But it didn't work for me because i have no-cache.
Here's What worked for me:
$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']);
if($pageWasRefreshed ) {
// Page was refreshed
}
else {
// Page wasn't refresh
}
Any water stones in this method?
Set a cookie for the first time when someone visits the page. So, when refresh check whether cookie exists or not, if yes the page is refreshed.
if (isset($_COOKIE['token']))
{
//The page is refeshed,
}
else
{
//first time user, set cookie.
setcookie('token');
}
You can do this multiple ways:
Set a session on the first time then check if the session is set or not.
Insert the client IP address in the MySQL database and check on every request if the user has visited before or not.
Now you have created your own logic and Google it how to implement this way
I suggest you use 2nd solution.
Does anybody know how to detect in PHP if meta refresh was made to visit our page?
I mean, some page have the following html code
<META HTTP-EQUIV='refresh' content='3; URL=http://www.example.com/mypage.php'>
And I want to detect in mypage.php if it was realy that way of redirection.
P.S. website with meta refresh is not my website, so I cannot pass any parameters
P.P.S. I don't know exact URL of the website with meta refresh, it can be anything.
P.P.P.S. I am not interested in origin of redirection, I am interested in the FACT of that redirection.
Just use sessions or cookies.
Check if it exists, elsewhere create it (to be detected on next refresh).
Some like:
session_start();
$currentPage = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if (empty($_SESSION['lastSeenPage']) {
if ($_SESSION['lastSeenPage'] == $currentPage) {
// Comes from Refresh
}
}
$_SESSION['lastSeenPage'] = $currentPage;
Maybe you need to check if that SESSION persists after visiting another pages.
You can check it (where "//comes from Refresh") with
if($_SERVER["HTTP_REFERER"]== $currentPage) {
// do the stuff
}
Whenever I go to a page i.e. login page or any other page, I want to save the name of the page in a $_SESSION variable.
login page:
<?php
session_start();
$_SESSION['page'] = 'login.htm';
?>
It works only for the login page and doesnt overwrite in other pages for e.g. home page:
<?php
session_start();
$_SESSION['page'] = "home.htm";
?>
I need the sesssion variable 'page' to hold the last page I was, can anyone help please?
Why not just use $_SERVER['HTTP_REFERER']? This will give you the previous page in PHP, without having to add anything to sessions.
when you navigate to a new page first retrive the saved "back" variable (and use it in a back link/breadcrumbs or something), and then overwrite the sessions "back" variable with the curent page, to have it ready for the next move =)
If all you need is default "back" functionality you should let the browser handle it.
If what you want is something to be used as a breadcrumb following some internal order (or path in a tree) my advice is to let each page "know" the path that leads to it.
If you really need to know from what page the user came from save it to a previous variable before you write over the current variable.
// Make sure user didnt just refresh the page
if ($_SESSION["current"] !== "currentPage.php") {
$_SESSION["previous"] = $_SESSION["current"];
$_SESSION["current"] = "currentPage.php";
}
You're using different keys.. 'page' and 'back'.
I have a system where the user logs in and is immediately directed to a PHP page that runs some queries which it inputs into session variables. After the PHP has finished executing, it redirects the user to the main page. I would like to know how I can set it up, so that when the user refreshes the main page, it redirects them to the preceding page where the calculations can run again before the main page is displayed. I simply cannot have the two pages merged together. Does anyone know how I can accomplish this?
EDIT: PHP include is the closest to what I need, but the problem is that when I use AJAX to submit a form, the session variables in the first file update with new data from the database. Those variables need to stay static until an actual page refresh by the user. Does anyone know how I can include the form but make it invisible to Jquery?
You could store a value to the $_SESSION and every other load, forward to the other page.
// Start session
session_start();
// If the session variable is set...
if (isset($_SESSION['loadCount']) {
// Increase the count
$_SESSION['loadCount']++;
// If it is even...
if ($_SESSION['loadCount'] % 2) {
// Reidrect
header('Location: /calculate-again.php');
die();
}
} else {
$_SESSION['loadCount'] = 1;
}
In the login form, make it submit to page_with_awesome_calculations_and_queries_to_session_variables.php. Assuming no input is sent in that page, make your calculations queries and sessions there, and then send a Location header to the new page.
header('Location: 'index.php');
This will cause the address bar in the browser to actually change to index.php, and a refresh will make the user stick there.
You should also secure your page_with_awesome_calculations_and_queries_to_session_variables.php to not allow access unless the correct $_POST variables are set.
Have mainpage.php receive one url parameter t which is the epoch timestamp.
Whenever calculation.php redirects to mainpage.php it will pass the epoch timestamp. If mainpage.php finds the epoch timestamp is older than five seconds, it will redirect back to calculation.php and exit.
mainpage.php
if (time() - $_GET['t'] > 5)
{
header('Location: calculation.php');
die();
}
else
{
// proceed normally
}
calculation.php
// perform calculation
header('Location: calculation.php?t=' . time());
Add
<?
require 'calculations.php';
// Mainpage goes here
?>
to the top of the mainPage. This makes it an extension of the mainPage which is run before the main page is loaded.
I have a website, and I have to implement (with PHP and/or JavaScript) an alert message that triggers two minutes after a visitor has entered the site. I've searched, but all solutions I've found are for an unique page. I need the timer counter to start when the user enters my site, no matter through which page. And I need that counter keeps counting while the user navigates my site's pages.
One solution could be using session variables. I can make a script that looks for this variable, if it doesn't exist means that the user is entering the site. Then I set this variable with current time. The script it's in each page, and it will be reading this variable via AJAX each x seconds and I'll know when the user is in my site since two minutes.
I don't know if it's right or not (I've not implemented yet), but I'm not pretty sure if session is the best way. If the user leaves the page but has other navigator windows opened, the session doesn't expire, and if he enters the site again, the counter will not be reset.
So, two questions:
Is there a better method to have
more control on the real entering
and exiting?
If not, is my above
approach right?
Thanks.
Something like this should work.
$alert_message = false;
if(!isset($_SESSION['time_entered'])){
$_SESSION['time_entered'] = time();
}
if($_SESSION['time_entered'] =< time() - 120){
if(!isset($_SESSION['message_sent'])){
$alert_message = true;
$_SESSION['message_sent'] = true;
}
}
And in <head>:
<?php if($alert_message):?>
<script type="text/javascript">alert("You've been here for at least two minutes.");</script>
<?php endif;?>
Also make sure that you have session_start() at the top of every script.
You don't need AJAX, you just need to store the time in a session variable, and then include some JavaScript on each page, here is an example:
<?php
session_start();
$time = microtime(true);
if (!$_SESSION['foo']) {
$_SESSION['foo'] = (microtime(true)+120);
}
?>
<script type="text/javascript">
var timeoutID = setTimeout(function() {
alert('two minutes have passed');
}, <?php echo bcsub($_SESSION['foo'], $time)*1000 ?>);
</script>
You will need some additional logic so that it does not keep firing after the 120 seconds are up.