Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    5
    Thanked 0 Times in 0 Posts

    How to: Php script that runs another script every x min.

    Hello,

    firstly I would like to know you that I am not so familiar with coding at all- got a few lessons in my university (studying design and fashion), so please be nice to me

    Well to my issue- I am working on my portfolio webpage made under WP, i currently made a working script (yay, so proud of myself) but I cant figure how to do, if I want to execute it on certain pages every x minutes.

    So what I wish and what is my idea: I would like to write a php script which will run my working script every x minutes when user visits specific pages.

    For example if user visits my page something.com/page1 my script will run, if user visits something.com/page2 immediately script will run in next x minutes minus elapsed time from previous execution. Basically I do not want to run script everytime when someone visits something.com/page1 or /page2, but maximally (60/x)times a hour.

    Hope I explained it well Thank you for your help

    Jane

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    you could use session.
    store time() in say $_SESSION['myTime'] variable and then
    PHP Code:
    if($_SESSION['myTime'] < time() - $yourInterval){
       
    //reset time in $_SESSION['myTime']
       //do your thing

    or you can simply store time() in cookie
    Last edited by patryk; 03-25-2013 at 01:45 PM. Reason: typos in vars ;)

  • #3
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    Well, use sessions and cookies to store the time elapsed between visiting pages. I would suggest setting up cron jobs on the fly to call the script but that would be a very complex process involving setting up daemons and might not be available on shared hosting.

    So why don't you just use Ajax to call the script after x minutes.

    Cheers.

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    Quote Originally Posted by Redcoder View Post
    I would suggest setting up cron jobs on the fly to call the script but that would be a very complex process involving setting up daemons and might not be available on shared hosting.
    cron jobs for one visitor? you can't possibly be serious. whatever crond triggers is ran in shell, hence you need a way to say what user you are serving, hence you will need cookies/session anyways, so whats the point if you can use cookies alone to do exactly the same thing? ;>

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    you could use session.
    store time() in say $_SESSION['myTime'] variable and then (...)
    Thank you for your quick reply.
    So, am I right, if I made it this way?:

    PHP Code:
    <?php
    $_SESSION
    ['myTime'] = storetime()
    // 600 as 10 mins.
    $interval 600
    if($_SESSION['myTime'] < time() - $interval){
    ***
    //reset time in $_SESSION['myTime']
    ***//do your thing
    exec ('/home/janawerid/web/.../.../.../')
    }* 

    ?>
    Jane

  • #6
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    yes only you can't do $_SESSION['myTime'] = time() on every page, becase that would cause if() to be skipped every time.
    and you have to do session operations befor everything else. once server sends headers, you can't mod cookies

    --edit--
    what are those "*" in your code?

  • Users who have thanked patryk for this post:

    Jana.Werid (03-25-2013)

  • #7
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    at the very begining of your script do something like that:
    PHP Code:
    session_start();
    $intervalElapsed false//just a flag
    if(!isset($_SESSION['myTime']){
        
    $_SESSION['myTime'] = time();
    }
    $interval 60//one minute
    if($_SESSION['myTime'] < time() - $interval){
       
    $intervalElapsed true;
       
    $_SESSION['myTime'] = time();

    and then wherever you want to run your every-some-time thing:
    PHP Code:
    if($intervalElapsed){
       
    shell_exec('/home/whatever/blah.sh');

    should work

  • #8
    New Coder
    Join Date
    Aug 2012
    Posts
    17
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    yes only you can't do $_SESSION['myTime'] = time() on every page, becase that would cause if() to be skipped every time.
    and you have to do session operations befor everything else. once server sends headers, you can't mod cookies

    --edit--
    what are those "*" in your code?
    Thank you so so much- I appreciate your help well, those stars- i do not know why they are here, maybe it is from copy-paste from my editor..

    And really THANK You

    * and wow- thanks for update :-*
    Last edited by Jana.Werid; 03-25-2013 at 02:39 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •