Web Analytics Made Easy -
StatCounter Will the php script continue to run after I redirect them ? - CodingForum

Announcement

Collapse
No announcement yet.

Will the php script continue to run after I redirect them ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Will the php script continue to run after I redirect them ?

    Hi,

    I am using a <meta http-equiv ='refresh'> to redirect
    the user to an external site. I also want to update my
    tables to reflect what the user clicked on.

    Can I do the update after I send the user ?

    Like this ...

    PHP Code:
    echo "<html>
            <head>
            <title>Transfering</title>
            <meta name=\"allow-search\" content=\"no\">
            <meta name=\"robots\" content=\"noindex,nofollow\">
            <meta http-equiv ='refresh' content = '
    $delay;url = $ad_link'>
            </head>
            <body></body>
            </html>"
    ;  

     
    // update databases
    $sql "UPDATE pages SET clicks = clicks+1 WHERE page_id = $page_no";
    mysql_query($sql) or die("could not UPDATE Page: $page_no"mysql_error());

    $sql "INSERT INTO `clicks` (broad_no,member_no,advert_no,block,offer_no,plat_no,page_no,link_no,comm_no,rel_no,cat_no,click_date,subject,ip_add,url)
    VALUES         ('
    $broad_no','$member_no','$advert_no','$block','$offer_no','$plat_no','$page_no','$link_no','$comm_no','$rel_no','$cat_no','$click_date','$subject','$ip_add','$url')";
    mysql_query($sql) or die("Could not INSERT Clicks from 3 Page Links."mysql_error());  

     exit; 
    Will this speed up the re-direct slightly ?

    Is there any problem doing it in this order ?

    Thanks.David.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    In theory yes - you've output the html before your script finishes however there are 2 issues to be aware of:

    1) The moment your users browser navigates away the connection to your server is broken and the script is terminated

    2) ISPs and php output don't always mix - the ISPs server will sometimes wait until the output has finished before passing it on to the end users browser. If this happens you're still stuck with inconsistency - on the one hand you can output your html and keep going but on the other it will be unreliable.

    The best way to deal with this is to use the ignore_user_abort(true) function which will tell apache / php to continue processing even if the user disconnects and then register_shutdown_function() to register a function to execute when the script is terminating.

    But to be honest, the easiest way would simply be to run the php logic first and then output the html. It'll happen so fast that there is little chance of the user clicking cancel and aborting the code.

    Finally you're using mysql_ functions in that code, you really need to upgrade because thats not even included in the latest versions of php. If you get forcefully upgraded to version 7 by your host you're code will no longer run. Thats the beneficial advice to you.. the bit you won't like and will see as nagging is that mysql_ functions are nowhere near as secure and thus you really need to upgrade just for the simple sake of keeping your database safe.
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      Thanks for the reply,

      1) The moment your users browser navigates away the connection to your server is broken and the script is terminated.

      Yes, this is the point ... Since I am navigating them away, does that in effect end the my script
      - before the table update in done

      ???
      If you want to attract and keep more clients, then offer great customer support.

      Support-Focus.com. automates the process and gives you a trust seal to place on your website.
      I recommend that you at least take the 30 day free trial.

      Comment


      • #4
        As I have said, the result varies and will be inconsistent. Yes in theory it should terminate the script but again that is not guaranteed - some browsers might take 1 or 2 seconds just to make sense of the html code and display it - and by that time your script is done anyway.

        If you're after a yes / no answer, it is both because of the behavious of different systems - that is why I have given you other ideas. I know it might not be want you want but it will be more reliable. The two functions I mentioned, I use in a script that serves download files - when a user disconnects the registered shutdown function is called, saves the position of their download (eg the percentage downloaded) and then quits. It's basically the same as you need - the user disconnects but the script can continue to do things with the database.

        PHP: register_shutdown_function - Manual
        "Tango says double quotes with a single ( ' ) quote in the middle"
        '$Name says single quotes with a double ( " ) quote in the middle'
        "Tango says double quotes ( \" ) must escape a double quote"
        '$Name single quotes ( \' ) must escape a single quote'

        Comment


        • #5
          Here's an example:

          PHP Code:
          register_shutdown_function('script_shutdown'$Token);

          //Do other stuff here - including your html output

          function script_shutdown($Token)
             {
             
          $Row mysql_fetch_array(mysql_query("select * from `:@:downloads_sessions` where `token`='$Token'"));
             
             (
          $Row['threads'] == 1)? mysql_query("delete from `:@:downloads_sessions` where `token`='$Token'"): mysql_query("update `downloads_sessions` set threads=threads-1 where `token`='$Token'");
             } 
          Obviously you'll need to re-work your own code to match this. The $Token variable is of mixed type - that is it can be a string, array, integer etc.. so you can pass your array of data which the function will then use at shutdown time.

          I've left this using mysql_ for you but you should really, really, really (yes really) update to mysqli at the minimum (which is so similar you'll barely notice the difference).
          "Tango says double quotes with a single ( ' ) quote in the middle"
          '$Name says single quotes with a double ( " ) quote in the middle'
          "Tango says double quotes ( \" ) must escape a double quote"
          '$Name single quotes ( \' ) must escape a single quote'

          Comment


          • #6
            Thanks,

            I guess I will have to upgrade sometime, but I think I have enough anti injection stuff
            and I run my dedicated server so I won't get 'forced' to upgrade to php 7.

            I am happy with php5

            BUt as I said, I will upgrade sometime ...
            If you want to attract and keep more clients, then offer great customer support.

            Support-Focus.com. automates the process and gives you a trust seal to place on your website.
            I recommend that you at least take the 30 day free trial.

            Comment


            • #7
              I read somewhere recently that php5 is still powering most of the php enabled servers out there. I suspect there is still millions of sites using mysql_ functions too.

              It took years for hosts to upgrade from php4 to php5 and the more diverse the interweb is becoming the slower these things are happening. IPv6 is rolling out incredibly slowly - at least here in the UK
              "Tango says double quotes with a single ( ' ) quote in the middle"
              '$Name says single quotes with a double ( " ) quote in the middle'
              "Tango says double quotes ( \" ) must escape a double quote"
              '$Name single quotes ( \' ) must escape a single quote'

              Comment


              • #8
                And some webhosts like the one I use allows the site owner to switch PHP versions. So a person can go back a revision if something fails. I know, it sounds crazy, because that is the point of upgrading PHP, to make it safer and more secure. Webhosts around the world are just as care-free as kitchen table scripters like myself. Who's minding the stove?

                Comment


                • #9
                  Originally posted by jeddi View Post
                  I am happy with php5
                  Hopefully you mean PHP 5.6 as that is the earliest version of PHP that still has any support at all (patch support for 5.5 ended in July so any security holes discovered in it that were still there then will always be there to be exploited).

                  At least with you running your own server only some of the security issues with your code can affect others as only those attacks that allow the attacker to use your server to attack others will work while those that can only affect accounts on the same server will only affect you.

                  PHP is at least as secure as most programming languages but has a bad security reputation due to people writing their own code to introduce security holes and then failing to keep their PHP version up to date (which for the most part users of other languages don't do).

                  As for mysql_
                  The mySQLi interface was introduced as a replacement in early July 2004. The PDO interface was introduced as a replacement in late July 2004. In July 2013 the mysql_ interface was declared to be obsolete and that it should no longer be used for new code and got rid of from old code as soon as possible. There has been plenty of time since July 2004 for everyone to dump the antiquated insecure mysql_ interface and only novice/amateur programmers would still be trying to use it now given that it was deleted in December last year. Professionals would have switched to one of the alternatives about ten years ago (at least for any new code to be written after that) and would have converted the rest in 2013 as advised.
                  Stephen
                  Learn Modern JavaScript - http://javascriptexample.net/
                  Helping others to solve their computer problem at http://www.felgall.com/

                  Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

                  Comment

                  Working...
                  X