Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77

    Unanswered: redirecting at the middle of script. possible?

    Is it possible to make a redirection in the middle of the script work when all headers are long-time sent?

    What I need to do is to check at the middle of the page some variable's value and depending on it automatically redirect current page to other one. Any solutions?

    TIA.
    Yours faithfully,
    Yaroslav Zaremba

  2. #2
    Join Date
    Jan 2003
    Posts
    21
    you can't do that, php is a server script, if the headers are already send, the client is beginning to get the page.

    Why don't you test your variables in the top of the page ?

    Another solution may be to put the headers in a string variable, to test for your redirection and to write the string after that.

    And the last solution is to put a javascript redirection in the middle of your page ... but it's not the great solution

  3. #3
    Join Date
    Dec 2002
    Posts
    65
    Like arfman said, if you have already sent the headers then you will not be able to redirect via PHP. If it's possible process all your calculations before outputting anything and work from there.

    If you'd like to provide more info no what you're working with maybe a more in depth solution can be given.

    -b
    (I'm only available at the email address provided in my profile on weekdays, if you have questions or advice, during off hours use AIM). Also any views I provide here or on my website are mine and not representative of any views of my work, family, friends and sometimes even myself.

    http://www.bcyde.com

  4. #4
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Well, it's kind a too complicated to describe the exact problem I've met with in coding but I do really need some automatic redirection!.. What about javascript's one? Could you please provide more info on this subject how can I do this? And why this is not that good method for problem solution?

    TIA.
    Yours faithfully,
    Yaroslav Zaremba

  5. #5
    Join Date
    Dec 2002
    Posts
    65
    Well the javascript function you want is window.location
    and it's usage is just

    window.location = "http://www.yoururl/";

    so I guess if you really must do it you can probably just echo out a script that just does:

    if ($bRedirect) //if we need to redirect
    {

    ?>

    <script language="javascript">
    window.location = "http://www.yoururl/";
    </script>

    <?

    }

    or if you don't like breaking in and out of script

    if ($bRedirect) //if we need to redirect
    {
    echo "<script language=\"javascript\">\n";
    echo "window.location = \"http://www.yoururl/\";\n";
    echo "</script>\n";
    }

    That should do it I think, I haven't tested it out, but if you have problems
    just leave another message here. As to why it's bad, well preferably stuff like redirects you want to keep out of the hands of the client. For example what happens if, say a user has javascript disabled on their browser, or has a browser that doesn't support a specific javascript feature you're using?

    Also, unless there's something really weird going on decisions like redirecting or denying access should be done first before any other work is done so that you don't do extra processing that will just be discarded.

    Hope this helps,
    -b
    (I'm only available at the email address provided in my profile on weekdays, if you have questions or advice, during off hours use AIM). Also any views I provide here or on my website are mine and not representative of any views of my work, family, friends and sometimes even myself.

    http://www.bcyde.com

  6. #6
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Thank you, works fine for me! Just added a little notice with the link for those who won't be able to redirect automatically.
    Yours faithfully,
    Yaroslav Zaremba

  7. #7
    Join Date
    Jan 2003
    Posts
    21
    why isn't it the great solution ?

    because if you make a php redirection, the client never get the first page, and don't have to download a page with a javascript redirection.
    A php redirection is on the server, which don't upload you the wrong first page but the second you have to be redirected.
    With a javascript redirection, you download the first page, your browser intercept the javascript redirection code and ask for it on the server and so you download 2 pages.

    The other problem is that your are browser "addict", if the browser has javascript disabled the redirection doesn't work, in php you don't have to hurry about that

  8. #8
    Join Date
    Sep 2002
    Location
    Kyiv, Ukraine
    Posts
    77
    Yes. I've got it. But even such software as these boards are written that way that you first get the page with information about new message adding from you and only after this automatically redirecting to the actual updated thread ... using javascript, I guess.

    I wanted something like this. And I've got it! Thanks!
    Yours faithfully,
    Yaroslav Zaremba

Posting Permissions

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