Test

How to ‘refresh’ a php page?

how do you get a php page to refresh or reload?
start right from the top again? thanks in advance from sunny clearwater fl.

  • 17 Contributors
  • 24 Replies
  • 18K Views
  • 7 Years Discussion Span
  • Latest Post 10 Years Ago Latest Post by uzor64

Cant you just do a javascript refresh?

header("Cache-Control: no-cache");

everytime the page is requested, a fresh copy is loaded. Is this what u r lookin for?

I dont like every time the page refresh. I want the page will be reloaded only once. can u help me to do that?

That must be because ur browser has a cache copy of the page.
browsers use this mechanism to enhance the surfing speed.
add this on top of ur page-

header(“Pragma: no-cache”); header(“cache-Control: no-cache, must-revalidate”); // HTTP/1.1 header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”); // Date in the …

Still its not working. The above code shown in output window. can u give me the full code to insert and retrieve image from the database by using blob.

its getting displayed in browser . Are you sure u placed these lines inside Why dont u post up …

All 24 Replies

I dont like every time the page refresh. I want the page will be reloaded only once. can u help me to do that?

header("Cache-Control: no-cache");

I dont like every time the page refresh. I want the page will be reloaded only once. can u help me to do that?

I am using php to get an image from database. when I get an image, the image will be shown after hitting the refresh button. pls help me to solve this

That must be because ur browser has a cache copy of the page.
browsers use this mechanism to enhance the surfing speed.
add this on top of ur page-

header(“Pragma: no-cache”); header(“cache-Control: no-cache, must-revalidate”); // HTTP/1.1 header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”); // Date in the past

This will instruct browser not to maintain the cache copy of this specific page.. and hopefully u wont need to hit refresh to display ur image.
Cheers!!

I am using php to get an image from database. when I get an image, the image will be shown after hitting the refresh button. pls help me to solve this

Still its not working. The above code shown in output window. can u give me the full code to insert and retrieve image from the database by using blob.

Still its not working. The above code shown in output window. can u give me the full code to insert and retrieve image from the database by using blob.

its getting displayed in browser . Are you sure u placed these lines inside Why dont u post up the code u r using and i shall suggest what modifications are required..

database blob image

Have messed this up b4
If the nocache code is getting displayed in the browser in the output window
Assuming the page displayed is «page.php»
page.php includes the line or something similar
The nocache code previously listed should be inserted in the of «page.php» not «getImageFromDatabase.php» else that code is output to the browser as well as (or instead of) the image
the nocache code should work

Читайте также:  Mysql and java example

database blob image

Have messed this up b4
If the nocache code is getting displayed in the browser in the output window
Assuming the page displayed is «page.php»
page.php includes the line or something similar
The nocache code previously listed should be inserted in the of «page.php» not «getImageFromDatabase.php» else that code is output to the browser as well as (or instead of) the image
the nocache code should work

hey almostbob,
correct me if m wrong but the header function is supposed to execute before displaying any output in the browser isnt it? then how are u supposed to place it inside head tag.
I believe it must go on the top of page which is used to display the image ie which outputs html for image display. watsay?
what you are trying to tell is the use of meta tag inside head which is also an option but doesnt works with IE. so the best way is to use http headers.

Источник

Refresh a page once using php

I want to read a lot of text files, extract some data and insert it to a MySql database. Here is my suggested workflow: When the user starts processing some data I create import.lock file, in this file we could write how many records we processed already AND the most important index of the last processed batch.

Refresh a page once using php

is there a way to refresh a page once in php? Like I want to put it in an if statement

I know header(«refresh: 3»); refreshes it every three seconds but I want something that only does it once.

You can use this code within your if condition

You just need to check the condition if its true then refresh the page.

Refresh a page once using php, 3 Answers 3 You just need to check the condition if its true then refresh the page.

Real Time Data Display Using JavaScript & PHP MySQL

Real Time Data Display From MySQL Database To Website | Update Page & Get Data Without
Duration: 5:56

Php auto refresh div jquery 3.3.1 interval fetch records mysql

phprefreshdiv #autorefreshdiv #fetchrecordsmysqlphp code auto refresh div and fetch records Duration: 8:40

Refresh Page Automatically

I will be creating a JQuery slide show, but need the page to refresh every 5 minutes to change the content.

I know I can do this via some Javascript, but this can be altered on the client side to avoid page refreshes. Is there a way for the server to timeout a page and force a refresh?

In addition to a javascript reload, you can send a refresh header:

header("Refresh: 300;url='http://thepage.com/example'"); 

The browser will redirect after 300 seconds regardless of javascript. It can be disabled in configuration of the browser though, but it’s not commonly disabled.

Meta refresh will work very simply:

The 300 means it will refresh every 300 seconds or 5 minutes. In this way you don’t use JavaScript so there is no way the user can turn it off.

There’s no way you can force the page to reload from the server.

The way to do it in JavaScript is simple:

Although this could be overridden on the client side, reguler users won’t be messing around with your script.

As a side note: Is there any reason you’re so adamant on reloading the page?

The JavaScript Method

var timer = null; function auto_reload() < window.location = 'http://domain.com/page.php'; >

PHP refresh page after outputting some HTML (breaking along operation into chunks)

I code mostly client-side these days, so my PHP is rusty and I would prefer a pure PHP design in this particular case.

Читайте также:  Django html form get

I don’t need any client -side code for this. I want to read a lot of text files, extract some data and insert it to a MySql database.

  1. this could potentially take quite some time, so
  2. I want to echo() some indication of progress. Also,
  3. I am concerned that it might take longer than the default 30 seconds, but I don’t like to mess with that limit, in case my script hangs.

In order to do #2, I would normally use header(‘Refresh:1’); , but what if that refresh happens in the middle of the MySql INSERT operation?

I suppose that before INSERT , and on page reload, I could just COUNT MySql table entries, to determine where begin processing the next text file.

Alliteratively, I could reload the page every few iterations of the loop, after the INSERT has been done, but how, since I could not use header(‘Location:X’); as I have already echo() ed the progress. Perhaps I could somehow fake a form submission? But, that feels hacky.

This seems to be a generic design pattern and I am sure that better brains than mine came up with an elegant solution long ago. Can someone tell me what that solution is? Preferably pure PHP. I have searched, but can’t find anything.

At the moment, I am going with a JS solution: echo ‘‘; , but would prefer a pure PHP

You might not consider this a pure PHP solution because there is an onload parameter specified for the tag. Then again, purity is not all it’s cracked up to be. But I throw this out as a technique you could use to give the user a running pseudo «progress bar» and your script would only get interrupted when you wanted it to and you would be able to pass back to the script any restart parameters. In this demo, the only «restart parameters» being passed back are successive integers that drive the progress bar, but you can get the idea:

 else < $progress = (int)$_REQUEST['progress']; >// increment progress: $progress++; // draw progress bar: $progress_bar = str_repeat ('+' , $progress); // simulate doing some work: sleep(5); // simulate being done or not: $done = $progress == 5; ?>      else < ?>  ?> Progress:  
"> else < ?>Done! ?>

For this, I would use import.lock file. Here is my suggested workflow:

  1. When the user starts processing some data I create import.lock file, in this file we could write how many records we processed already AND the most important index of the last processed batch. It’s up to you how you create batches creating strategy, you can implement it with temporary .json files, for example, join 3 big files and split it into small chunks.
  2. I assume you already implemented files reading and chunking, so for example we can make foreach and every iteration put some data into MySQL.
  3. So as I said before every iteration we updating import.lock with processed chunk number. At the moment when foreach ends we deleting import.lock file or clearing it.
  4. Every iteration we refresh the page with a simple PHP refresh and after refresh reading the import.lock file, taking the last imported batch index, and start again from next.

For progress to implemented you count all created junks (chunk1.json, chunk2.json . ), and let’s say you we 100 chunks and we processed 15 already, import.lock have a value of 15 so it means 15% done. After refresh we taking 16 chunks (chunk16.json).

After every junk is done we delete it from file system.

Implementation example

Here is my implementation example, tested and working: Link to the repository: https://github.com/RomkaLTU/php-progress-case You still need to implement JSON files generation. I have used session in this example to track progress as user Mawg says reinstate Monica suggested.

And you may need to adjust header(«refresh: 2») and chunk size.

session_start(); $dataDir = __DIR__ . DIRECTORY_SEPARATOR . 'data'; $chunkFiles = array_values(array_diff(scandir($dataDir), ['..', '.'])); $chunksCount = count($chunkFiles); $currentChunk = $_SESSION['currentChunk'] ?? 1; // need to identify current index of proccessing file // need to follow naming convention here or make something smarter $currentChunkIndex = array_search('chunk_' . $currentChunk . '.json', $chunkFiles); if (isset($chunkFiles[$currentChunkIndex])) < $chunkFilePath = $dataDir . DIRECTORY_SEPARATOR . $chunkFiles[$currentChunkIndex]; $dataJson = file_get_contents($chunkFilePath); $data = json_decode($dataJson, true); foreach ($data as $item) < // @TODO do something with a $item >unlink($chunkFilePath); $_SESSION['currentChunk'] = $currentChunk + 1; if (!empty($chunksCount)) < header("refresh: 2"); >else < $_SESSION['currentChunk'] = 1; >> ?>        

Ideally a script that «could potentially take quite some time» must not be executed on the fly via HTTP. The best practice is to queue the job, the http request only add the job into the queue and does not run it directly, so it is a fast and predictable operation that will not timeout, then the job is run independantly of the HTTP server, the job is executed by a queue manager with larger or no timeout on resources. You can periodically poll via HTTP from client side (with HTTP refresh or Javascript) to display some progress status to the user.

A minimalist but perfectly viable queue manager could be a cronjob launching a simple PHP cli script that read and write some persistent locked data about jobs (file, database, . ). For more complex use cases (prioritizing or distributing load), it may be useful to use a dedicated queue manager software.

If cron is not an option, you may also run a PHP CLI script daemonized as a 100% pure PHP queue manager.

To main concept here is to decouple running unpredictable or long jobs from serving HTML pages through stateless HTTP in a predictable and fast way.

Refresh a page using PHP, You sure can refresh a page periodically using PHP: This will refresh the page every three seconds.

PHP Refresh Page

Currently I have a problem with GoDaddy and its hosting. The maximum amount of time allowed for a PHP script to run is set to 120 seconds = 2 mins.

Sometimes when I run my script, it takes more than 120 seconds and sometimes it takes 30 seconds.

When it takes over 120 seconds, I get a Internal Server Error (500) .

The question I have is, is it possible to figure out if a script run time is at 110 seconds and then refresh the page so that the Internal Server Error does not occur.

If what ever is causing your script to run long is some sort of self contained blocking function (like a database query), then you are kind of screwed.

If not, say you are in a very large loop, then just do the math your self.

At the very start of your script, save the current time: $t_start = time();

Then at the top of your loop, do a $runtime = time() — $t_start; then you have how long you have been running, and can use that to break your loop gracefully if nearing your 120 limit.

PHP — auto refreshing page, //set a header to instruct the browser to call the page every 30 sec header(«Refresh: 30;»);. It does not seem to be NECESSARY to pass the page

Источник

Оцените статью