How to after x seconds run a routine and redirect to other page?

Hi there,

i need a code in a a file msg.php, per example, after x seconds of displaying a warning, it change a value in the database and redirect the page for index.php, per example?

how to do this the best way?

Best regards,
Who is Participating?
kanchan_karjeeConnect With a Mentor Commented:
If this is your requirement
-- Display a warning message to user say for 10 sec.
-- Redirect the user after database updatation etc.

Then you could try using ajax.
update.php is the script where you update database.
Waiting time can be modified as requiered

<script type="text/javascript">
var redirect = false;
var waitingTime = 0;
var messageDisplayTime = 10;
function updateDBTask() {
      var xhr;
      if (window.XMLHttpRequest) {
            xhr=new XMLHttpRequest();
      } else {
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
      xhr.onreadystatechange=function() {
            if (xhr.readyState==4 && xhr.status==200) {
                  redirect = true;

function Redirect() {
      if (redirect == true) {
            if(waitingTime >= messageDisplayTime) {
                  document.write('<scr'+'ipt type="text/javascript" >location.href="index.php"</scr'+'ipt>');
<h2>Warning message here...</h2>
var t = setInterval("Redirect()",1000);
i don't know what's the BEST way, but you can try this :

// display warning here
 ...your routine goes here...
sleep(10); // in seconds
// change a value
....insert your own routine here (I dunno what's you're trying to change)....
// go to index.php
header('Location:'); // or whatever location you need
if its for browsers then you need to do it diffrently

display the warning page then have a js & meta-refresh tag to redirect to another page (or same page) with extra query param to then apply the DB change
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.