www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Is there a maximum amount of header redirects allowed?

  1. #1
    Join Date
    Jun 2007
    Posts
    400

    Is there a maximum amount of header redirects allowed?

    I have a process set up which basically runs through a bunch of import pages, and once a page is finished it moves onto the next. I've been redirecting with the header as such...

    Code:
    header("Location: newpagehere.php");
    But it keeps hanging up. At first I thought the page it was dying on was at fault, but after about an hour of going over and over it I decided to just start the import 2 pages in, and sure enough... it hung 2 pages further. After a bit of testing around I realized no matter where I start it, I get exactly 20 header redirects before it won't do any more. This is the error message it gives me...

    Code:
    Redirect Loop
    
    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
    
    The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.
    
        * Have you disabled or blocked cookies required by this site?
        * NOTE: If accepting the site's cookies does not resolve the problem, it is likely a server configuration issue and not your computer.
    Apparently it thinks it is stuck in a neverending loop so it just stops after 20? But I need about 60 page redirects or so to run this thing. Is this something that can be changed in PHP configuration or is it a Firefox issue? Either way, do I need to change my whole approach or is there an easy way to solve it? All of the pages are already built and need to also be able to be run individually, it would be a very large hassle to have to start combining them or something.

    PS. There are no cookies in this code.
    Last edited by xvszero; 12-04-2008 at 11:56 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    I won't admit to understanding exactly why you want/need all these redirects, but it seems to be you might be better off with a succession of include()'s and/or require()'s instead of redirects, perhaps?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2007
    Posts
    400
    Well I don't fully understand either, I'm still kind of an amatuer. But essentially we have about 50 or so imports that all have their own PHP pages, and we need an automated process that runs one after another every night. I have all that working except, as stated, it dies after 20 or so redirects.

    I suppose includes would work just the same though? Albeit its a couple hour long process and then the original page would need to be running the whole time... is that a problem? I've had some issues in the past with very long pages just dying randomly, despite putting in very long max execution times. Then again I always suspected that was server hickups, which would kill the process regardless.

  4. #4
    Join Date
    Jun 2007
    Posts
    400
    The includes seem to work but now the importtracking function doesn't pass the individual page names. It just passes the main page that has included it.

    Code:
    importtracking($_SERVER['PHP_SELF']);
    Is there any way to get it to pass the individual page names without having to type each one in individually?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Perhaps:
    PHP Code:
    importtracking(__FILE__);
    //...or...
    importtracking(basename(__FILE__)); 
    But still I really don't know what's going on. My gut instinct is that whatever all these separate pages are doing should be in one function (or class, perhaps), then that function would get called in a loop through an array or database result set that would provide the parameters needed for each function call.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Oct 2006
    Posts
    364
    I think you're doing something like this:


    index.php - got to index2.php
    index2.php - go to index3.php
    index3.php - go to index.php

    what are you doing to need 60 redirects!?

    about:config

    int network.http.redirection-limit default(20)

    changing that should do the trick. I doubt your users would change it though.
    Last edited by Tabo; 12-05-2008 at 06:46 PM.

  7. #7
    Join Date
    Apr 2007
    Posts
    1,664
    You might was to consider some kind of task server to complete this reliably.
    A database could store the path to the scripts to run and whether it has been completed.

    Table Columns for taskserver
    taskid path completed

    //psuedo code
    SELECT * FROM taskserver WHERE completed=0

    create an array of uncompleted taskids

    loop the array, include the file and update the table

    include "path";
    //script runs then update taskserver
    UPDATE taskserver SET completed=1 where id="taskid"

    You can build an array with parts of the query in the loop dynamically, to only update one time rather than on each iteration. Google multiple update mysql.

    Have the taskserver script run at a scheduled time with crontab and on boot in case the server went down in the middle of a task.
    Anti Linux rants are usually the result of a lack of Linux experience, while anti Windows rants are usually a result of a lot of Windows experience.

  8. #8
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    The number of ways to resolve this issue is undoubtedly unfathomably enormous ... but, I too am really confused as to what your end goal is ...

    In any case, for weird things, use AJAX. Or pseudo-AJAX:
    HTML Code:
    <script type='text/javascript' src='/path/to/step1.php'></script>
    Then have each step dynamically add a script tag to trigger the next step. So, step1.php would print the following JavaScript (and no non-JavaScript)!
    HTML Code:
    var temp_node = document.createElement('script');
    temp_node.type = 'text/javascript';
    temp_node.src = '/path/to/step2.php';
    document.body.appendChild(temp_node);
    Will something like that suit your needs?
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  9. #9
    Join Date
    Jun 2007
    Posts
    400
    Quote Originally Posted by NogDog View Post
    Perhaps:
    PHP Code:
    importtracking(__FILE__);
    //...or...
    importtracking(basename(__FILE__)); 
    But still I really don't know what's going on. My gut instinct is that whatever all these separate pages are doing should be in one function (or class, perhaps), then that function would get called in a loop through an array or database result set that would provide the parameters needed for each function call.
    I understand that logic, but really each import is so specialized that it'd be a very, very... VERY long function. Tons of what the company manager calls "massaging" the data going on in specific ways for each import. It's easier to just keep them separated.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Sounds like an excellent candidate for object-oriented coding. You might have one (or a few) abstract classes with any common functionality defined in its methods, then abstract methods for the functionality that needs to be separately tailored for each variation. Then each of your separate files would become class definitions which extend that abstract class and which would be instantiated within the master script. Possibly a factory pattern would be useful here.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Nov 2008
    Posts
    2,477
    ^^^ my thoughts exactly, you are discovering first hand why procedural coding is not so well suited to large and complex problems as a well thought out OO approach.

  12. #12
    Join Date
    Jun 2007
    Posts
    400
    I kind of vaguely remember OOP from school.

    I think though, when I say a bunch of imports, I am somewhat giving off the wrong impression. Though they are imports, there are so many specific things going on besides the actual import that for the fact that there ARE a limited amount (I think it's actually closer to 25-30 than the 60 I originally thought) it is probably still an easier approach to just handle each one by one than set up objects.

    For instance, on the phone marketing import I have to first find all the customers that will be excluded from being imported into the table. So you have to grab from one table to see if a customer has a "do not call" etc. request connected to them. Then you need to exclude all customers in the bankruptcies database, because we're not allowed to contact customers in bankruptcy proceedings. Then you exclude those connected to dealers we no longer support... that's another table. Then there are a few more exclusions from a completely different database... well, 4 different databases. I can't really get those into our current database without redoing a lot of design done before I got here and well, this project is *supposedly* supposed to be done by Christmas, so that's not happening.

    So now we can finally import from the customer + account + a few other tables with fields we need... into the phone marketing table for each of our two companies.

    And then we use those to create what are called "non" phone marketing tables (I have no idea why, the name is very deceiving) which are, essentially, taking any customer who is a customer of one of the companies but NOT of the other and giving that data to the other so they can market to that customer.

    This whole design is a bit different than say... importing into the delinquent account tables. Which you would *think* would be as simple as calculating who is delinquent and who isn't, but yet again there is a lot more specific stuff involved in determining what "delinquent" even means.

    So while a lot of this could all be set up with classes and objects, most of it is very specific stuff that simply isn't done on the other imports. The only obvious similarity is that, eventually, fields get imported into a database. But that part is just a few lines of code, the "massaging" is the bigger part. Is it really worth it to go OOP when the imports, as it stands, are all already working on their own pages?

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    Here's an off-the-wall and off-the-top-of-my head idea: have your main script call each of the others via cURL. This way each script is still processed in a stand-alone mode as when you do the redirects, but the main script never redirects. You might, however, want to have it flush some output between each call so that the browser doesn't think it timed out.
    PHP Code:
    <?php
    function myCurl($url)
    {
       
    $ch curl_init();
       
    curl_setopt($chCURLOPT_URL"http://www.example.com/");
       
    curl_setopt($chCURLOPT_HEADER0);
       
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
    $output curl_exec($ch);
       
    curl_close($ch);
       return 
    $output;
    }

    $scripts = array(
       
    'script1.php',
       
    'script2.php',
       
    'etc.php'
    );

    foreach(
    $scripts as $script)
    {
       
    $result myCurl('http://localhost/directory/'.$script);
       echo 
    "<h3>Result for '$script':</h3>\n$result";
       
    flush();
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  14. #14
    Join Date
    Jun 2007
    Posts
    400
    I honestly don't even know what cURL is, though it more and more seems like something I am going to need to figure out. Do I have to configure the server a specific way to use it? My boss sort of fired the server guy and I'm hesistant to mess with server configuration unless it is very simple.

  15. #15
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,923
    It's normally enabled in most PHP installations. You could either check the output of phpinfo() to see if it's enabled, or just do a quick test:
    PHP Code:
    echo (function_exists('curl_init')) ? 'Yes' 'No'
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles