1

I want to reload or refresh my complete PHP file for this I used

header("refresh: 3600");

it refresh the page after 1 hour only if this page is open in browser. But I want it to refresh by self if page is not open. This file is on cPanel and server is always on.

I also tried this

<meta http-equiv="refresh" content="3600">

but my problem is still there that it will not refresh the page if it is not open in browser.

  • 1
    You want to refresh page which is not open?? (which doesn't exist) ?? Probably you want to call some piece of code, you cannot refresh page which doesn't exist, check about cronjobs – MorganFreeFarm Aug 20 '19 at 06:27
  • 1
    you must use cronjob. https://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – Saeed M. Aug 20 '19 at 06:27
  • i have a page which get data from api and show it some where i want to refresh page after 1 hour while that page is not open in browser but it exist in my cpanel – Muhammad Ahtisham Aug 20 '19 at 06:30
  • @SaeedM. is there any other option instead of cronjob? – Muhammad Ahtisham Aug 20 '19 at 06:32
  • 1
    @muhammad no. If you want to execute a specific php script every hour, the only option is to run a cronjob. You can't "refresh a page" if there is no "open page".. – Ermenegildo Aug 20 '19 at 06:35
  • You can't actively refresh the view interface that is not under your control. The ```header('refresh: 3600')``` tells the browser to actively refresh the page after an hour. This is actually the browser resending the request to your server. You are passively responding to data changes instead of actively refreshing. If you need to refresh the data in cpanel for an hour, then your question may be "How to periodically refresh the page in cpanel"? Or you can use ```web_socket```, which allows you to actively contact the client from the server, but it costs a lot – Luna Aug 20 '19 at 06:49
  • @Ermenegildo thanks i will get more knowledge about cronjob. – Muhammad Ahtisham Aug 20 '19 at 06:51
  • *"is there any other option instead of cronjob"* ... What is your problem with cron jobs ? they are very easy to create and almost every free/paid host will allow them ? – Accountant م Aug 20 '19 at 06:54
  • @Luna thanks for the clarification i will follow it. – Muhammad Ahtisham Aug 20 '19 at 06:55
  • @Accountantم i tried to learn cronjob but i was not picking it up. so i was searching for something else. Thanks to all as it is only option so i will learn more about it – Muhammad Ahtisham Aug 20 '19 at 06:59
  • @MuhammadAhtisham You have to learn them, there is no escape from them, you will encounter them again in your career. There must be a graphical interface for them by hosting services, cPanel, etc . if no GUI, just `crontab -e` in terminal and add your cron line `1 1 * * * php -f /path/to/script.php` check [this](https://stackoverflow.com/a/55385788/5407848) also – Accountant م Aug 20 '19 at 07:08
  • @Accountantم please let me know what will be the command for this file .. the path is given below.. i'm getting confuse in this do not know why. please help me. public_html/live_cricket/API/my_file.php – Muhammad Ahtisham Aug 20 '19 at 07:13
  • @Accountantم i'm using 0 * * * * php -f /public_html/live_cricket/API/my_file.php want to execute every hour of every day . is it right command? – Muhammad Ahtisham Aug 20 '19 at 07:15
  • @MuhammadAhtisham What is the **absolute** path to your PHP script, starting by `/` because this is a relative path `public_html/live_cricket/API/my_file.php` ? – Accountant م Aug 20 '19 at 07:15
  • @MuhammadAhtisham the time is correct but the path is not correct – Accountant م Aug 20 '19 at 07:16
  • @Accountantم C:\xampp\htdocs\Live_Cricket\API\news_agg_api_curl.php got it using realpath(). so command will be like 0 * * * * php -f C:\xampp\htdocs\Live_Cricket\API\news_agg_api_curl.php – Muhammad Ahtisham Aug 20 '19 at 07:22
  • 1
    @MuhammadAhtisham ohhh, noo you are on a windows machine, cron jobs are for Linux only :( I'm sorry man. I think it is the time for you to start dual booting a Linux with your Windows. – Accountant م Aug 20 '19 at 07:25
  • check [this](https://active-directory-wp.com/docs/Usage/How_to_add_a_cron_job_on_Windows.html) it could help in the mean time. – Accountant م Aug 20 '19 at 07:32
  • Possible duplicate of [Set Up Cron Job To Execute PHP](https://stackoverflow.com/questions/22702506/set-up-cron-job-to-execute-php) – yivi Aug 20 '19 at 07:49
  • No need to dual boot as Accountant suggested. Windows Scheduled Task is the windows equivalent of a Cron job. – ADyson Aug 20 '19 at 08:04
  • @Accountantم that was the localhost file /user/bin/php /home/user/public_html/live_cricket/API/news_agg_api_curl.php made this command on cpanel .. as we can add cornjobs from cpanel. – Muhammad Ahtisham Aug 20 '19 at 08:07
  • @ADyson yes but now i'm doing it on cpanel. and see my above comment . is that command is right? – Muhammad Ahtisham Aug 20 '19 at 08:08
  • I cannot know where you placed your files in your disk. If it runs, then it's probably right. If it doesn't, then it probably isn't – ADyson Aug 20 '19 at 08:13
  • @ADyson did not understand your comment .. it's on cpanel in public_html and then live_cricket folder – Muhammad Ahtisham Aug 20 '19 at 08:15
  • You asked if the command is right. I don't know if the command is right because I don't know your file structure. It's even possible the PHP executable could be in another location. I can't see your disk to tell you yes or no to your question. The best way to know if it's correct is to try and execute the command. Does it work if you run it, or not? – ADyson Aug 20 '19 at 08:18
  • 1
    /usr/local/bin/php /home/frapplications/public_html/live_cricket/API/news_agg_api_curl.php i used this command and it runs. Thank you all of my seniors(you all) to help me. – Muhammad Ahtisham Aug 20 '19 at 08:22

1 Answers1

1

As you have been told in comments, cron jobs on Linux or Windows Task Scheduler on Windows is the natural solution for your problem.

However there is a workaround that you can do to avoid them in the mean time, you can make script that triggers your page every 1 hour

trigger.php

<?php
ini_set('max_execution_time', 0); //unlimited
$iterations = 0;// allow only 100 iterations, increase if you need
while ($iterations <= 100){
    $iterations++;
    file_get_contents("http://yourdomain/Live_Cricket\API\news_agg_api_curl.php");
    sleep(3600); // sleep for 1 hour
}

You will have to execute it the first time from your terminal/CMD

php -f C:\xampp\htdocs\path\to\trigger.php

Note: you must execute this file from the PHP CLI (like I did ), not from a browser, to avoid Apache's Timeout directive which will end the execution after the Timeout period if you opened trigger.php from the browser. And execute it every time you reboot your machine.

Also note that file_get_contents will not work if allow-url-fopen is disabled. you can also use curl for that.

Don't forget to remove header("refresh: 3600");.

Accountant م
  • 4,969
  • 2
  • 30
  • 49
  • Thank you for your precious time you really help me alot. i used cornjob o cpanel and add on using this command /usr/local/bin/php /home/frapplications/public_html/live_cricket/API/news_agg_api_curl.php – Muhammad Ahtisham Aug 20 '19 at 08:20
  • @MuhammadAhtisham Great, did the cron job run as you expect ? also remember that developing on Windows and executing on Linux does not create a good environment for you. I started by XAMPP on my Windows just like you but I had to use Linux to better understand and learn my production server. – Accountant م Aug 20 '19 at 08:28
  • how can i paste screenshot here? i want to show you my cronjob which i added . it's not working. */5 * * * * /usr/local/bin/php /home/frapplications/public_html/live_cricket/API/news_agg_api_curl.php this is the cron job. which execute after every 5 mints but it's not working – Muhammad Ahtisham Aug 20 '19 at 08:51
  • this news_agg_api_curl.php update a json file . when i run this file on brower it update the file and i see the modified time of that json file. but cornjob is not updating it.. like modified time should change after every 5 minutes – Muhammad Ahtisham Aug 20 '19 at 08:53
  • In cPanel cron jobs interface, there is a place to set **your email**, put your email in this place, so cron will send you an email with the output of the execution of the command, so you can see the error. – Accountant م Aug 20 '19 at 09:02
  • Could not open input file I'm getting this error i searched on it and change my command accorfing to it. like placing -q after /php etc and also change the permission of file to 777 but getting same error. can you help me in this? – Muhammad Ahtisham Aug 20 '19 at 13:13
  • This error could be from inside your script, which means your script is running, it is a common error when running scripts from cron because the **current directory** is possibly changed , check [my answer here](https://stackoverflow.com/a/55385788/5407848) . try to change your cron line to this `*/5 * * * * cd /home/frapplications/public_html/live_cricket/API/ && /usr/local/bin/php /home/frapplications/public_html/live_cricket/API/news_agg_api_curl.php` – Accountant م Aug 20 '19 at 19:29
  • 1
    Thanks a lot my problem is solved by doing these following things : 1) command is :/usr/local/bin/php -q /home/frapplications/public_html/live_cricket/API/news_agg_api_curl.php 2) to include some file : set_include_path('/home/frapplications/public_html/live_cricket/CURL/'); require 'curl.php'; 3)to add file you need to add complete address like : $my_file="/home/frapplications/public_html/live_cricket/files/myfile.json" – Muhammad Ahtisham Aug 21 '19 at 10:08