Results 1 to 7 of 7

Thread: php redirect

  1. #1
    Join Date
    Jul 2004
    Posts
    494

    Unanswered: php redirect

    I'm making a script for inserting html pages into a database table. It's a 700-page ebook convert from pdf to html with the frames of page links on the left and navigation on the top.

    Within the pages, I added textareas and a submit button within a form. Everything works. THe problem is that I need to do this manually clicking each page link on the left frame 700 times.

    Since the forms on each page direct to a page where it inserts into the database table I need to put a redirect somehow that will lead to the next page. For example:

    I have
    http://localhost/ebooks/pg_0004.php

    And I want to go to:
    http://localhost/ebooks/pg_0005.php

    I came up with the following script to go incrementally
    http://localhost/ebooks/pg_0005.php
    http://localhost/ebooks/pg_0006.php...


    I'm open to any other suggestions on the script but how do I redirect to the next page automatically after the inserting into the database is finished?
    PHP Code:
         $myarray explode('000'$previousurl);
            foreach(
    $myarray as $value)
                {
                    
    $urlsnip $value;
                }
            
    $thestringa $urlsnip[0] . $urlsnip[1] . $urlsnip[2] . $urlsnip[3] . $urlsnip[4];        
            
    //echo $thestringa . '<br />';            
            
    $thestringb $urlsnip[0] + $urlsnip[1] . $urlsnip[2] . $urlsnip[3] . $urlsnip[4];
            
    //echo $thestringb . '<br />';
            
    $newurl str_replace($thestringa$thestringb$previousurl);
            
            echo 
    $newurl;
            
            
    header'Location: ' $newurl ) ;
    exit; 
    Compare bible texts (and other tools):
    TheWheelofGod

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Just a quick note:

    Calling " echo $newurl; " before using the header() function will stop the header from working. The reason being, at that point in time you've already sent the headers. (Echo'ing is output to a browser effectively).

  3. #3
    Join Date
    Nov 2007
    Posts
    14
    True, but sometimes its a pain trying to prevent output before sending headers, so...

    Add ob_start(); above the rest of your code - it will send all output to buffer until the script finishes processing or until you flush the buffer -> essentially means you can send a header at any point within your script.

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    the ob_* functions all have a larger amount of overhead to them however and should only be used in special circumstances.
    Also if you are planning on capturing all output before you send your headers (i.e. using ob_*) then you should seriously consider redesigning your code and performing ALL presentation of html AFTER logic, this means AFTER sending your headers.
    Imagine finding a header in the middle of a piece of code that does lots of echo's. It's just plain bad practise. Separate your layers. Model->View->Controller is your friend (MVC). If you don't know what i'm talking about then i suggest you google it.

  5. #5
    Join Date
    Dec 2007
    Location
    Detroit
    Posts
    7
    Why are you doing it in such a strange way?

    Couldn't you use one of the many freely available tools to convert the pdf to html and then run through those files?

    I'm sure you could parse a pdf file into separate html pages and then use the php dir object to iterate through those files and insert the html into a DB.

    Maybe i'm not understanding what you're doing and apoligize if I have.

  6. #6
    Join Date
    Nov 2007
    Posts
    14
    Quote Originally Posted by aschk
    (i.e. using ob_*) then you should seriously consider redesigning your code and performing ALL presentation of html AFTER logic, this means AFTER sending your headers.
    Imagine finding a header in the middle of a piece of code that does lots of echo's. It's just plain bad practise. Separate your layers. Model->View->Controller is your friend (MVC). If you don't know what i'm talking about then i suggest you google it.
    a) Well of course they have a "larger amount of overhead"... but, this is plain text being sent into what is normally at least an 8mb buffer.
    b) I didn't say I do this... I was merely making a suggestion to resolve the OP's issue, whether or not it was a good suggestion is an entirely different matter.
    c) Unfortunately, for those unlike you and me who don't manually code and use Dreamweaver plugin's to write their code for them, this is often the only way to resolve their issues.
    Last edited by Genx; 01-13-08 at 09:23.

  7. #7
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    I completely agree. It's a good workaround for the situation that they are currently in, and I praise your optimism over my pessimism.

    I think the basic rule/answer should be; to avoid having to write more code to buffer your output, or perform some sort of bizarre action, just DON'T echo anything before you do a header().

    How i hear you ask? Well how about storing anything you echo into an array instead.
    e.g.
    Code:
    while(loop){
      // do stuff
      // add the thing we were previously going to echo value to array
      $arrOfValues[] = $newurl;
    }
    
    ... later on use that array of stuff.
    Of course this shouldn't really be an issue in the original shown code, becuase you shouldn't NEED to echo ANYTHING if you know you're doing a header() redirect anyway.

Posting Permissions

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