Every 4 seconds I am refreshing a php page and it updates the time.
Each time this happens I want to send the time to another page but NOT navigate away from the page
that is refreshing the time...
Below is some sample code:
<?
$target = mktime(10, 0, 0, 0, 0, 0) ;
header('refresh:4; url=timer.php');
$today = (int)(time ());
$siteToReceiveTime = "http://www.nbit.co.za/toriggo/receive.php?time=$today";
print "time in $today seconds";
?>
There are a couple of ways that you could do this...
Ajax being one - that could handle the every 4 seconds bit and would be able to make requests without changing the page.
The other option would be with php curl...
Read the CURL function documentation. You can have PHP call CURL to load the page for you from the PHP script without interfering with your users.
file_get_contents("http://www.nbit.co.za/toriggo/receive.php?time=$today");
Related
I have a function which adds a new blog post to my database.
After running add_new_post() function the post should be approved by my another function approve_post().
But I don't want to approve post immediately. I want to run approve_post() function in time between 1 to 5 minute (random).
Now I am using this code:
function add_new_post() {
*my code of adding post*
$time = rand(60, 300);
echo $time;
sleep($time);
approve_post();
}
But browser shows loading until sleep ends. Also, I found that my Windows machine shows time execution error.
Is there a way to run approve_post() function in the background without showing page loading to the browser?
I would be grateful for а code example.
First, you need to increase the maximum execution time of a PHP script by using the following link https://www.plus2net.com/php_tutorial/max-ex.php
Then use sleep() function to delay code execution as:
<?php
echo date('H:i:s');
sleep(15);
flush();
echo "<br>";
echo date('H:i:s');
?>
Reference link
Or you can CronJob
There are multiple options.
You can use ajax which is required to open a page or you can use a Cron job. I will prefer to use Cron job
The concept will same, for an example. The post table must has created_date ,approve_date and approve_status. So when you create the post it will save the approve_date too.
Example, You create a post on 18:00 then the approve_time will 18:05. Then the background process (ajax or Cron job) will check the approve_status first then execute the approve process when live time same like approve_time
ref : cron job & Ajax
sorry for my English
This may seem like a simple thing but I just can't get my head around it. How do I use php to display a div on a website at a specific time for only a duration? Eg. Show object for 10min every hr.
Ok, I have an object ie ahajsjajshaksjaksjiajsns which is displayed on a website when visitors visit the site. But I don't want it to show all the time but say every hr. And to disappear from the website after 10min.
if(date("i") < 10) echo "...";
This code will echo ... every hour from minute 0 to minute 9. e.g. 8.00-8.09, 9.00-9.09, 10.00-10.09, ...
PHPs date function is able to give you values you can check against a specific date. For more information see the docs.
I haven't coded anything web in a few years, so forgive me if I'm rusty but:
You can't do this all serverside. The page will compile but unless the user refreshes the page, the object in question will be forever available.
What you want to do, is use kekub's example to generate the following code only if it's within the time range, but also include JavaScript to destroy it when time is up:
$time = date("i");
if($time < 10){
$timeToExpire = 10 - $time;
echo "<div id="yourObject">I will expire soon!</div>;
echo"<script type = 'text/javascript'>setTimeout(function() {
$('#yourObject').fadeOut('fast');
}, ".$timeToExpire * 10000.");</script>"; // * milliseconds e.g 6 minutes
}
I haven't tested it but what should happen is, the webpage will generate the div and also the code needed to hide it when the time is up (say there is only 6 minutes left to show it).
Although I think personally you should do this all in Javascript.
you can set time for two divs using setTimeout and can call divs in that function
setTimeout(function(){page2(mintime)},10000);
setTimeout(function(){page1(mintime)},10000);
This kind of function usually should not be done in server side. Anyway if you want to achieve this using PHP you can have something like this:
while (true) {
$result = yourfunction;
if (resultIsGood) {
break;
}
sleep(3);
}
You can sleep in a loop
How can I determine the loading time of an external page using PHP?
For example, finding the loading time of https://stackoverflow.com/questions.
If it is on another server, then it is basically the same way you would time something else, only instead of calling a locally defined function, you use file_get_contents:
$t = microtime( TRUE );
file_get_contents( "your tested url" );
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
As a warning, that will also include the time it takes to make the request and receive the request (time spent over the wire). Unfortunately, unless you actually have access to that server, that cannot be helped.
Now, if you're trying to get the render time of your site, you can use ob_start and ob_end_clean:
$t = microtime( TRUE );
ob_start();
// do all of your rendering
ob_end_clean();
$t = microtime( TRUE ) - $t;
print "It took $t seconds!";
Measuring page load times must be done on the client. Implementing a full browser using PHP is not a trivial task.
Do you want to measure the page load times of some remote website?
Or do you want to instrument your own site to measure page load times?
Do you really mean page load times or do you mean the time taken to process each request?
If you're talking about request times do you mean the time taken from receiving the request to delivering it to the client
or just the time for generating the response?
I would like to make my PHP script freeze at a screen for at least 5 seconds before loading into the next script. I have tried "Sleep()" however that is not the function I am looking for as it only pause the script that is "going" to be loaded.
Here are my codes:
echo "Welcome ",$_SESSION['username']."<br>";
echo "Click here to Logout : ".'<br>Logout';
sleep(10);
header("Location: http://192.168.11.32/phploginserver/test.php");
echo '<script type="text/javascript">window.location="test.php"</script>';
}
I would like the echo'to another page' to be delayed for at least 5 seconds before loaded so that my users can view their user name before being automatically redirected to the next page.
$time = new DateTime();
$newtime = $time->Modify("+5 seconds");
while($newtime > (new DateTime()))
{
// Do nothing
}
You cannot freeze PHP script at a screen.
Just because there are no PHP scripts at screen. But merely HTML, rendered by browser.
Such a freezing considered bad practice and don't used nowadays.
Either show a message, if it's really important, or get user to the destination immediately (preferred).
You can use some AJAX-powered tips, as stackoverflow does.
so that my users can view their user name
Don't they know it already?
Can't they see it on the next page?
What if a user got disturbed and do not notice that message?
You can use an additional parameter with the php header to delay it. Like this:
header('Refresh: 10; url=http://192.168.11.32/phploginserver/test.php');
Originally, I just want to verify that session_start locks on session. So, I create a PHP file as below. Basically, if the pageview is even, the page sleeps for 10 seconds; if the pageview is odd, it doesn't. And, session_start is used to obtain the page view in $_SESSION.
I tried to access the page in two tabs of one browser. It is not surprising that the first tab takes 10 seconds since I explicitly let it sleep. The second tab would not sleep, but it should be blocked by sessiont_start. That works as expected.
To my surprise, the output of the second page shows that session_start takes almost no time. Actually, the whole page seems takes no time to load. But, the page does take 10 seconds to show in browser.
obtained lock
Cost time: 0.00016689300537109
Start 1269739162.1997
End 1269739162.1998
allover time elpased : 0.00032305717468262
The page views: 101
Does PHP extract session_start out of PHP page and execute it before other PHP statements?
This is the code.
<?php
function float_time()
{
list($usec, $sec) = explode(' ', microtime());
return (float)$sec + (float)$usec;
}
$allover_start_time = float_time();
$start_time = float_time();
session_start();
echo "obtained lock<br/>";
$end_time = float_time();
$elapsed_time = $end_time - $start_time;
echo "Cost time: $elapsed_time <br>";
echo "Start $start_time<br/>";
echo "End $end_time<br/>";
ob_flush();
flush();
if (isset($_SESSION['views']))
{
$_SESSION['views'] += 1;
}
else
{
$_SESSION['views'] = 0;
}
if ($_SESSION['views'] % 2 == 0)
{
echo "sleep 10 seconds<br/>";
sleep(10);
}
$allover_end_time = float_time();
echo "allover time elpased : " . ($allover_end_time - $allover_start_time) . "<br/>";
echo "The page views: " . $_SESSION['views'];
?>
That seems to be a firefox related "issue". If you request the same url in two tabs/windows the second request waits until the first request is finished (could also be an addon that blocks the second request, haven't tested that).
Take e.g.
<?php // test.php
$start = microtime(true);
echo "<pre>start: $start</pre>";
sleep(5);
$end = microtime(true);
echo '<pre>', $start, "\n", $end, "\n", $end-$start, '</pre>';
I called it twice and the output was
start: 1269742677.6094
1269742677.6094
1269742682.609
4.9995958805084
and
start: 1269742682.6563
1269742682.6563
1269742687.6557
4.9994258880615
Note that there's already a 5 second gap between the start times.
When called as http://localhost/test.php and http://localhost/test.php?a=b instead of the exact same url twice this does not happen.
Both IE8 and Chrome do not show that behavior.
Yes, This could be because of session_start() blocking other requests in the same session (file based). I was able to verify the issue in Firefox(4.x) and Chrome(10.x) in Windows XP/PHP 5.2 with default session handler(file). I am not sure if this issue is reproducible for non-file session handlers.
obtained lock
**Cost time: 9.90100598335**
Start 1303227658.67
End 1303227668.57
sleep 10 seconds
allover time elpased : 19.9027831554
The page views: 4
This is a very interesting issue and the Firefox tab locking described in the above answer would have eclipsed this one from being detected.
http://www.php.net/manual/en/function.session-start.php#101452
Since php does not have a container. How do two calls to same session get serialized? Who does this? How do the two processes talk? Is PHP module always active and only spawning threads after doing session check? In that case the PHP module is indeed behaving like a container that is, in this case, providing session management service to this extent.