I have a set of pages with the following names....
update1.php
update2.php
update3.php
update4.php
update5.php
update6.php
update7.php
At the moment, I have all 7 open on Chrome and refreshing every 30 minutes using the following code.
<body onload="setInterval('window.location.reload()', 1800000);">
I want to stick all these pages together so that I need only 1 page open. Is there a way to make a page refresh every 5 mins, but load a different section each time?
I was thinking of doing something like this, but it seems a bad way of doing it...
$refresh = $_GET['refresh'];
if (!isset($refresh)) { //update1 stuff; //reload page with &refresh=2 }
elseif ($refresh == 1) { //update1 stuff; //reload page with &refresh=2 }
elseif ($refresh == 2) { //update2 stuff; //reload page with &refresh=3 }
elseif etc.....
Any ideas?
In your php code that outputs the page html code, try something like this:
<?php echo ($_GET['refresh'] % 7) + 1; ?>.php // Name of next page
?refresh=<?php echo ($_GET['refresh'] % 7) + 1; ?>"; // Name of next page + 1
// All together
<body onload="setTimeout(function() { window.location.href = 'update<?php echo ($_GET['refresh'] % 7) + 1; ?>.php?refresh=<?php echo ($_GET['refresh'] % 7) + 1; ?>';}, 1800000);">
This will open the new page with an incremented number after the specified time interval. The modulo operation helps us run through the pages in a consistent loop.
References
window.location.href
setTimeout()
You can Put All Scripts in one page and can use
<meta http-equiv="refresh" content="1800" /> in head
to refersh browser every after 30 Minutes
Related
im trying to write a loop that displays the counter on one line, been sitting here for over an hour but cant figure it out.
The main loop is
while($counter< 100){
echo $counter;
usleep($timeInSeconds*1000000);
$counter=$counter+1;
}
Now this prints 100 numbers after a delay each on a new line. Is it possible for the echo to instead replace itself for each loop?
I tried many options, here is one that didnt crash:
while($counter < 100){
$counter=$counter+1;
echo $counter;
usleep($timeInSeconds*1000000);
flush();
ob_flush();
}
However, with this option it works in one line with a delay, but it doesnt clear the previous echo, so its just a bunch of number next to each other
Could someone help me out?
You are trying to do something on the server that should be done on the client.
I expect you are making a timer. You should write some JavaScript code instructing the web browser on how to display multiple numbers with a delay in between. Your current code will show a loading wheel and a blank screen for the entire duration on many browsers.
Instead, replace your loop with something like:
var time_in_seconds = 1; // You can replace 1 with the value of the PHP variable
var count_element = document.getElementById("example_counter");
var n = 0;
var interval_id;
function update_counter(){
n += 1;
if (n >= 100) {
clearInterval(interval_id);
}
count_element.textContent = n;
}
interval_id = setInterval(update_counter, time_in_seconds * 1000);
<span id="example_counter"></span>
i'm quite a beginner with PHP and i tried to make something to get xp when cliking the button. You just need to click and it gives xp, then it refresh the page to refresh the player's stat on screen.
<form method="post">
<p><input type="submit" value="Kill the mob" name="add20xp" /></p>
</form>
<?php
if (isset($_POST['add20xp']))
{
$add20xp =("UPDATE users SET exp = (exp + 20)");
$execadd20xp = mysqli_query($connection, $add20xp);
echo '<meta http-equiv="refresh" content="0.1" />';
}
?>
The problem is that i want to prevent the user from smashing the button to prevent bugs and things like that... I tried to put sleep(1) but i can just keep spamming, wait the seconds and it works so it's not very useful.
Thanks for the help !
Save the last time the update was done in session state. Then, only allow the button to be pressed after (last time + 2 seconds) (Two seconds was chosen since that was the suggested interval in your original question).
if (isset($_POST['add20xp'])) {
if (!isset($_SESSION['last_post'])) {
$_SESSION['last_post'] = 0;
}
$currtime = time();
if ($currtime > ($_SESSION['last_post'] + 2)) {
$_SESSION['last_post'] = $currtime;
// ... process the post.
$add20xp =("UPDATE users SET exp = (exp + 20)"); // fix this line
$execadd20xp = mysqli_query($connection, $add20xp);
echo '<meta http-equiv="refresh" content="0.1" />';
}
}
As #Martin noted above in his comment, you want to do the update only for the user who pressed the button, which is the meaning of the comment "fix this line."
If you want to disable the button for 3 seconds after the form is submitted you can use this:
if(sessionStogare.getItem('submitted') === true){
document.querySelector('input[type="submit"]').disabled = true;
setTimeout(function(){
document.querySelector('input[type="submit"]').disabled = false;
sessionStorage.removeItem("submitted");
}, 3000);
}
document.querySelector("body").onclick = function() {
sessionStorage.setItem("submitted", true);
};
We will note the submission in the sessionStorage and check, if the form has been submitted every time we load the page. Then, we will disable the button and enable it after 3 seconds.
Change your php page to this:
// the beginning of the page:
<?php
// start a SESSION
session_start();
// setup a $_SESSION variable
if (!isset($_SESSION["timestamp"]))
$_SESSION["timestamp"] = 0;
//
// now put the $_POST part
if (isset($_POST['add20xp'])) {
// check the time
$now = time();
if ($now - $_SESSION["timestamp"] > 2) {
// more than 2 seconds have passed since last $_POST
// update the time
$_SESSION["timestamp"] = time();
//
$add20xp =("UPDATE users SET exp = (exp + 20)");
$execadd20xp = mysqli_query($connection, $add20xp);
//
echo '<meta http-equiv="refresh" content="0.1" />';
exit;
} else {
// nothing, just let the page load like it is.
}
}
?>
Notice some important changes:
the use of $_SESSION vars -> these vars are stored and can be
retrieved at every page load -> you can use them to store the last
time an action took place
the $_POST part should be at the beginning
of the page -> otherwise after you send a form, you load the page ->
check the post -> then reload... it's not efficient
if you put the $_POST part at the beginning, you actually don't need the page reload with the meta tag -> because the data are already
updated
Right now im trying to code read other codes and make it work, because some of the projects that i have are made by other developers and i really have to learn reading other code, I already made this one work but the problem is when i try to incorporate this pagination code to a $_GET variable an error occur, when it load the first page everything thing is smooth but when i click the other pages the $_GET variable dies. i already found a code in the site but i can't really get how to incorporate it with the other code.
here is the code that "MIGHT" solve the problem solving pagination $_GET
printf('Next',$targetpage,http_build_query(array('page' => 2) + $_GET));
here is the part of the code that make the link 1 2 3 4. . . and so on
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1?srId=$srdd'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage?srId=$srdd'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x?srId=$srdd'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage?srId=$srdd'>></a> ";
// echo forward link for lastpage
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages?srId=$srdd'>>></a> ";
printf("<a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages?srId=$srdd'>>></a>");
} // end if
/****** end build pagination links ******/
?>
I already figured it out guys I saw lots for this problem on the web and also some on the forum here's how to solve it
<?php
session_start();
// store session data
if (isset($_GET["srId"]))
{
$_SESSION['variable']=$_GET["srId"];
$srdd = $_SESSION['variable'];
}
else
{
$srdd = $_SESSION['variable'];
$tableName = $srdd."r";
}
?>
thanks for your response guys.
You will need to use either session variables or cookies to pass them one page to another.
The $_GET global array will be a NEW array for every new page, assuming you're sending a GET request and it can be seen in the URL.
In general, the web is stateless which means it will not remember what you do from one page to another UNLESS you use cookies, sessions or a database for persistence.
Design your links
Page 1 | 2 | 3 | 4
to have the information in the link so they are ready for the next page
Page 1 = http://url?page=1&page_limit=25
Page 2 = http://url?page=2&page_limit=25
...
Etc
If the paginated links are in this format, these variables will be in the $_GET array.
Try replacing
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1?srId=$srdd'><<</a> ";
with
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&srId=$srdd'><<</a> ";
After the initial '?' in a url, the following *'$_GET[]'* items should be separated from the preceeding one with an '&'. Note that your 'srId=$srdd' is preceeded by a '?'.
Example:
myurl.com?id=1234&page=help
Doing it this way will help you avoid having to store the information in the $_SESSION array, and keeps the URL secure.
In this code I'm trying to ban client if he/she/it doing to much(10) login request for 3 minutes. The problem is after 3 minutes user must refresh the page 2 times. I can see the reason why it's enter into if statement but I can't find the solution. I feel like I've overcoded.
if($this->sessions->get_data("wrong_login")>10){
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time());
}else
{
if(time() - $this->sessions->get_data("ban_time") > 180){ // 180 seconds
$this->sessions->remove("ban_time");
$this->sessions->remove("wrong_login");
}
}
// The message if user still banned
die("Banned for 3 minutes!");
}
I hope I can tell the problem..
EDIT: This code is the inside of the construct of register controller.
Before your IF statement, add another if statement that checks for ban_time session if the time is up, then set the wrong_login session to 0 if it is.
if($this->sessions->get_data("ban_time") < time())
{
$this->sessions->remove("ban_time");
$this->sessions->set_data("wrong_login", 0);
}
remove your else statement there.
also forgot to mention! when you set the ban time, it should be time() + 180.
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time()+180);
}
use header function.
e.g.
header("Location: /path/to/some/file.php?error=Banned for 3 minutes.");
Then on the file.php you can do this:
<?php
// Parse error
$error = isset($_GET['error']) ? $_GET['error'] : '';
// Display error (if any) and stop executing the rest of the code.
if (!empty($error)) {
exit($error);
}
?>
This will not work if you already started to output...
Currently I'm tracking time spent by user on website using PHP code mentioned below:
if (!isset($_SESSION[timeset1]))
{
$one_val = time();
$_SESSION[timeset_dummy]= $one_val;
$two_val = time()+1;
$_SESSION[units_all] = array
(
"year" => 29030400,
"month" => 2419200,
"week" => 604800,
"day" => 86400,
"hr" => 3600,
"min" => 60,
"sec" => 1
);
}
else
{
$two_val = time();
}
$diff = abs($two_val - $_SESSION[timeset_dummy]);
foreach($_SESSION[units_all] as $unit => $mult)
if($diff >= $mult)
{
$output .= " ".intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));
$diff -= intval($diff / $mult) * $mult;
}
I want to give pop-up to users after 8 mins of inactivity that session will expire in next 2 mins. Can you please suggest how can I show pop-up (preferably without using Javascript, nice to have with CSS3 & HTML5) . Pop-Up will have warning message "Do you want to continue" and one button "Yes" , if button is not clicked for 2 mins automatically page logout script (PHP) will be executed.
Any pointers to to get this logic implemented.
I found a jQuery plugin that looks like it will make your life easier. It is called jquery-idleTimeout.
The plugin has a few configuration items so you can customize it for your own needs…
inactivity: 1200000 //20 Minute default (how long before showing the notice)
sessionAlive: 300000, //5 minutes default how often to hit alive_url, we use for our ajax * interfaces where the page doesn’t change very often. This helps to prevent the logout screen of your app appearing in ajax callbacks. If you set this to false it won’t send off.
alive_url: ‘/path/to/your/imHere/url’, //send alive ping to this url
redirect_url: ‘/js_sandbox/’, //Where to go when log out
click_reset: true, //Reset timeout on clicks (for ajax interface) – resets the sessionAlive timer, so we are not hitting up your app with alive_url if we just did an ajax call for another reason.
logout_url: ‘/js_sandbox/timedLogout/index.html’ //logout before redirect (url so you can completely destroy the session before redirecting to login screen)
Here is a link to the github page to download the library.
https://github.com/philpalmieri/jquery-idleTimeout
Edit
Something I also noticed while looking at the source code, they are using jquery-ui as their stylesheet to make it look like it does in the demo.
As far as I know, you cannot achieve what you are looking for without javascript. The browser needs javascript to know when to open the pop-up. You can use whatever means you want to check for timeout, either the basic window.setTimeout or a more advanced library (like the one in tehAon's answer).
Since I cannot post a comment I'm going to ask here: your code seems awfully complicated for checking if a user is still active. Is there any particular reason you could not use something like this:
$_SESSION['last_activity'] = time();
function check_if_logged_in() {
if(time() - $_SESSION['last_activity'] > 600) { // 10 minutes but you could use 480 for 8 minutes
// Do redirect or take other action here
}
}
if(time() - $_SESSION['timestamp'] > 900) { //15 minute subtract new timestamp from the old one
$_SESSION['logged_in'] = false;
session_destroy();
header("Location: " . index.php); //redirect to index.php
exit;
} else {
$_SESSION['timestamp'] = time(); //set new timestamp
}
This is quite easy with php session variable.
set $_SESSION variable with timestamp and check with the action time
<?php
session_start();
if(time()-$_SESSION['time']>600)
unset($_SESSION['time']);
else
$_SESSION['time']=time();//updating with latest timestamp
?>