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
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    When a header("Location: $url") doesn't work

    In some point of my code i have a redirection to a diferent page.


    The problem is that sometimes that page doesn't exist.
    I want to detect that error, log it and fix it.

    In the manual the function definition is


    int header ( string string [, bool replace [, int http_reponse_code]])

    but when i call the function it never returns anything.

    i'm running php 4.2.2


    How can i detect if the page doesn´t exist? i've thougt of fopen but in that case it has to connect twice to page.
    Can someone help... thanks

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    well apache should redirect you to your 404 error page?

    but I suppose you can check.....
    PHP Code:
    <?
    if(file_exists($location)){ //or is_file//
     
    header("location:$location");
    }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the problem is that $location is a remote file /address and those file function do not work remotely

    i think it will only work with fopen....

    and what does header() return?
    Last edited by jcma; 01-20-2003 at 01:03 PM.

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The curl() function might help with this, http://www.php.net/manual/en/ref.curl.php .

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts
    the header tag MUST be executed before anything else
    PHP Code:

    <?php
     
     
    // Will Work:
     
    header("Location: $location");
     
     
    // Wont Work:
     
    echo "<html>";
     
    header("Location: $location");

    ?>
    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jcma
    and what does header() return?
    Good question acutally, and I couldn't find anything related to that in the manual (haven't looked closely enough probably). Anyway, I doubt that it returns the status code of a page the client fetches after receiving a header("location: "). Because this doesn't make sense to me - how should the PHP function immediately know of the result? The page requested by the browser could go down just in this microsecond and the client (browser) does not communicate synchronously with the server(PHP). It seems to me that the int returned denotes whether the sending of the header, i.e. the correct execution of the header() function, was successful.

    Your only options are to check if the file exists prior to redirecting the browser to it. You can do that with fopen(), the cURL functions, or use a socket (if you really want to do that very low-level), as others have told you already.

  • #7
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i haven't used cUrl because i havent installed it.
    but i tried fopen and fsockopen(or something like that) and they can't open remote files. but do open localhost files.

    maybe because i'm behind a firewall, but don't they use port 80? when i used fsockopen i told it to use :80.

    is this just my problem??

    this open a file and reads its title
    PHP Code:
        $file fopen ("http://$url""r");
        echo 
    "Done<br>";    
        if (!
    $file
        {
            echo 
    "<p>Unable to open remote file.\n";
         exit;
        }
        while (!
    feof ($file)) 
        {
            
    $line fgets ($file1024);
            
    // /* This only works if the title and its tags are on one line 
            
    if (eregi ("<title>(.*)</title>"$line$out)) 
            {
                
    $title $out[1];
                break;
            }
        }
        echo 
    $title;
        
    fclose($file); 

    and this one just tries to open a file(i think)

    PHP Code:

    $up 
    fsockopen("www.google.com"80, &$errno, &$errstr30);  
    if(
    $up
    {  
        echo 
    "ok";  
    }
    else
        echo 
    "$errsts"
    and outputs Bad file descriptor


    none of this code was made by me, so i think it's my problem....

  • #8
    New Coder
    Join Date
    Jan 2003
    Location
    Lisbon / Portugal
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i've just tested the fopen code above on my firewall /webserver and the code works perfectly.....
    it's good because it works and it's bad because i wanted it to work on my pc (behind the firewall/webserver).

    is there a way to solve this problem?

    i've seen a piece of code with fsockopen to connet to a site throuh a proxy, but my server doesn't have a proxy...


  •  

    Posting Permissions

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