Solved

Reload PHP page if file exists or time is 9am

Posted on 2009-05-09
9
559 Views
Last Modified: 2012-05-06
Hi,

I need to add some code to the head of a PHP file. What it needs to do is to check every 10 minutes if a file located in the same folder i.e loadfile.txt - if the file exists then refresh the page. If it doesn't then check the time. If the time is 9am or 10am then reload the page. Basically this code needs to do a check against the time 9am or 10am and also if a file exists. If any of those parameters are correct then reload the page. It also needs to do these checks every 10 minutes or so, so it needs to loop?
0
Comment
Question by:alex4544
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 24343271
The best way to do this is by using AJAX to get the time & check if the file exists, and reload your page accordingly.

Kind regards,

Matthias Vance
0
 
LVL 3

Expert Comment

by:khr2003
ID: 24343342
hi
Do you want the page to be refreshed when it is visited? if so, you can make a simple php function that checks the time of the server and do whatever you want with it. If not, then try to use the cron jobs in the c-panel, see this link for a good explanation:

http://www.trap17.com/index.php/cron-jobs-cron-jobs-cpanel_t6321.html
0
 
LVL 2

Author Comment

by:alex4544
ID: 24343388
What I want is some code in the head of my document that checks the time and if a file exists and will refresh the page if the variables are fullfilled. I need the code to run every 10 minutes or so without refreshing the page so some kind of loop or similar would be good. If someone can show me how the code works in AJAX or another language then that is fine but the page needs to be in PHP
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 24343447
You can find more information about XmlHttpRequest here (also check the links section underneath):
http://www.w3schools.com/XML/xml_http.asp

Once you know how to get data from PHP pages using this, you can write a php script that checks if the page should be reloaded. ie: let it return 1 if the page should be reloaded. Compare this in your javascript and reload the page.

I hope this helps you in the right direction.

Kind regards,

Matthias Vance
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:jimhap
ID: 24344965
Here is my PHP code. Save this as ajaxstatus.php.

This is the file that will be called by the web page (AJAX request).

Stay tuned for the next code (HTML AJAX integration)...
<?php

// Written by jimhap

// This is the file called by AJAX, returns 1 or 0

// if file exists or it is 9/10 AM.
 

// Variables - change to your needs
 

// File to check if it exists...

$FILE = "filename.txt";
 

if(file_exists($FILE)) {

  echo "1";

} else {

  if(date('h A') === '9 AM') {

    echo "1";

  } else {

    if(date('h A') === '10 AM') {

      echo "1";

    } else {

      echo "0";

  }

}

Open in new window

0
 
LVL 1

Accepted Solution

by:
jimhap earned 170 total points
ID: 24344967
Oops, made a little mistake, this SHOULD be correct:
<?php

// Written by jimhap

// This is the file called by AJAX, returns 1 or 0

// if file exists or it is 9/10 AM.

 

// Variables - change to your needs

 

// File to check if it exists...

$FILE = "filename.txt";

 

if(file_exists($FILE)) {

  echo "1";

} else {

  if(date('h A') === '9 AM') {

    echo "1";

  } else {

    if(date('h A') === '10 AM') {

      echo "1";

    } else {

      echo "0";

    }

  }

}

Open in new window

0
 
LVL 1

Expert Comment

by:jimhap
ID: 24345107
OK, add this into your page. If the page is PHP, add ?> before the HTML and
<? after the HTML to continue the PHP, if any.
<script language="javascript" type="text/javascript">

// Client script - access PHP file, get results, and determine if refresh

// or not.
 

// Get the HTTP Object

function getHTTPObject(){

if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

else if (window.XMLHttpRequest) return new XMLHttpRequest();

else {

alert("Your browser does not support AJAX.");

return null;

}

}
 

// Change the value of the outputText field

function checkOutput(){

if(httpObject.readyState == 4){

  if(httpObject.responseText == '1') {

    window.location.reload();

  }

}

}
 

// Implement business logic
 

function doWork(){

httpObject = getHTTPObject();

if (httpObject != null) {

httpObject.open("GET", "ajaxrequest.php, true);

httpObject.send(null);

httpObject.onreadystatechange = checkOutput;

}
 

// 10 minutes update

setTimeout ("doWork()", 600000);
 

}
 

var httpObject = null;
 

</script>

Open in new window

0
 
LVL 1

Assisted Solution

by:jimhap
jimhap earned 170 total points
ID: 24345113
Oops, forgot to close quotes.

Updated client code (copy + paste to desired page):
<script language="javascript" type="text/javascript">

// Client script - access PHP file, get results, and determine if refresh

// or not.
 

// Get the HTTP Object

function getHTTPObject(){

if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");

else if (window.XMLHttpRequest) return new XMLHttpRequest();

else {

alert("Your browser does not support AJAX.");

return null;

}

}
 

// Change the value of the outputText field

function checkOutput(){

if(httpObject.readyState == 4){

  if(httpObject.responseText == '1') {

    window.location.reload();

  }

}

}
 

// Implement business logic
 

function doWork(){

httpObject = getHTTPObject();

if (httpObject != null) {

httpObject.open("GET", "ajaxrequest.php", true);

httpObject.send(null);

httpObject.onreadystatechange = checkOutput;

}
 

// 10 minutes update

setTimeout ("doWork()", 600000);
 

}
 

var httpObject = null;
 

</script>

Open in new window

0
 
LVL 2

Author Closing Comment

by:alex4544
ID: 31579738
thats excellent. I can read the code and see what is happening so I will make my changes now. Your a star :D
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now