Web Analytics Made Easy -
StatCounter Php redirect - CodingForum

Announcement

Collapse
No announcement yet.

Php redirect

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

  • Php redirect

    Hi there,

    I have 3 files...

    index.php ...
    PHP Code:
    <?php
    include "http://www.allsortshop.com/site.php";
    if (
    $online == 0) { 
        
    header('Location: /offline.php'); 
        die(); 
    }
    ?>
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
    <title>Allsortshop Coming Soon</title>
    </head>
    <body>
    <div align='center'><img src='temp.png' /></div>
    </body>
    </html>
    offline.php ...

    PHP Code:
    <?php
    include "http://www.allsortshop.com/site.php";
    if (
    $online == 1) { 
        
    header('Location: /index.php'); 
        die(); 
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Allsortshop is Offline</title>
    </head>
    <body>
    ALLSORTSHOP IS CURRENTLY OFFLINE. CHECK BACK SOON! 
    </body>
    </html>
    And site.php ...

    PHP Code:
    <?
    $online 
    1;
    ?>
    Basically, in site.php, if online = 0, then my index.php page should redirect to the offline page, and if online is set to 1, then my index.php page should display.

    But whatever i set online to in site.php, it still redirects even if i set online to 1?

    Please help??

    Thanks!

    @Andy92 / Dribbble

  • #2
    When you include() a URL you get what the web server outputs, which in the case of your site.php script would be a blank page. If you can do so, try including the file through the filesystem. If all 3 files are in the same directory in the server then

    PHP Code:
    include ('site.php'); 
    will do fine.

    Comment


    • #3
      Edit: Took me a while to type this, basically says the same as above^^^

      I'll assume that these three files are on the same web site? If the site.php file is on a different web site, then this won't work as the site.php file will be parsed by PHP before being sent (you will only get any echo/print/print_r that is output by it.)

      Because you have specified a URL in the include "http://www...." instead of just a file path/file name, this is probably making a HTTP request to get the file. The issue above is probably means the $online variable does not exist, and the logic would test NULL == 0 and redirect to the offline.php file.

      You want to use a file path/file name and not a URL.
      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

      Comment


      • #4
        Ok.

        In my main index.php page i have put at the very top...

        PHP Code:
        <?php
        include "http://www.allsortshop.com/site.php";
        ?>
        Then in my site.php file i have put...

        PHP Code:
        <?php
        $online 
        1;
        if (
        $online == 0) { 
            echo (
        "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
        <html xmlns='http://www.w3.org/1999/xhtml'>
        <head>
        <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
        <title>Allsortshop is Offline</title>
        </head>
        <body>
        ALLSORTSHOP IS CURRENTLY OFFLINE. CHECK BACK SOON! 
        </body>
        </html>
        "
        );
            }
        ?>
        So basically, in site.php, online = 1 at the moment, so that when i go to index.php it displays the page i want.

        But when i change online to 0 in site.php, then when i go to index.php, it displays the ALLSORTSHOP IS OFFLINE text, but it still displays the index.php main page underneath.

        I know i need to put a die(); tag in there somewhere so that when online = 0, it will kill anything that gets displayd underneath that tag, but can you possibly tell me where to put it???

        @Andy92 / Dribbble

        Comment


        • #5
          to answer where you need to put the die()
          PHP Code:
          <?php
          $online 
          1;
          if (
          $online == 0) { 
              echo (
          "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
          <html xmlns='http://www.w3.org/1999/xhtml'>
          <head>
          <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
          <title>Allsortshop is Offline</title>
          </head>
          <body>
          ALLSORTSHOP IS CURRENTLY OFFLINE. CHECK BACK SOON! 
          </body>
          </html>
          "
          );
             die();
              }
          ?>
          but the
          if ($online == 0) {
          can never evaluate to True in that code.

          regarding the include statement --> in my opinion, your include statement is correct. after the include line, $online should be 1.
          but depending on your settings (like if you are on windows and if allow_url_fopen is enabled), http inludes might not be working --> this is often disabled because it creates serious securityissues when you for instance grab the include value from the querystring as some forum-software used to do.
          It's also possible that, if site.php is placed on another server, that short tags are not allowed there (use <?php instead of <?).

          i would however recommend to initiate the variable before the include, like
          PHP Code:
          $online 0;
          include 
          "http://www.allsortshop.com/site.php";
          if (
          $online == 1) { 
          ... 
          Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

          Comment


          • #6
            Ok,

            If i put the online = on the top of every page, i will have to change every page to shut down the whole site when i need to.

            Also, i have put the die(); where you told me to put it, so this is the code in the site.php file...

            PHP Code:
            <?php
            $online 
            0;
            if (
            $online == 0) { 
                echo (
            "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
            <html xmlns='http://www.w3.org/1999/xhtml'>
            <head>
            <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
            <title>Allsortshop is Offline</title>
            </head>
            <body>
            ALLSORTSHOP IS CURRENTLY OFFLINE. CHECK BACK SOON! 
            </body>
            </html>
            "
            );
            die();
                }
            ?>
            And it displays the offline message on the index.php page, but it also displays everything on the index.php page too!

            Have a look at...

            http://www.allsortshop.com

            ??
            @Andy92 / Dribbble

            Comment


            • #7
              I just tested this and if you read what PassiveSmoking and myself wrote, you will get an explanation of what is going on, and how to correct this -

              Because the include statement uses a URL to access the site.php file, this file is being requested using a http request. When this happens, the site.php file is being parsed through PHP and the HTML output is sent and inserted where the include statement is located. The die() statement is being executed and it stopped the execution of the site.php script, but the HTML output from it was already sent.

              If you change your include to using a file path/file name, so that the file is included directly, then it works as expected. The die() statement is then encountered during the execution of the main file code and execution of the script stops and the output following it is not output to the browser.
              Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

              Comment


              • #8
                Ok, i have put my main server path and it all works!!

                Thanks for all your help!!

                THANK YOU!!!

                @Andy92 / Dribbble

                Comment


                • #9
                  Originally posted by CFMaBiSmAd View Post
                  I just tested this and if you read what PassiveSmoking and myself wrote, you will get an explanation of what is going on, and how to correct this -

                  Because the include statement uses a URL to access the site.php file, this file is being requested using a http request. When this happens, the site.php file is being parsed through PHP and the HTML output is sent and inserted where the include statement is located. The die() statement is being executed and it stopped the execution of the site.php script, but the HTML output from it was already sent.
                  No. This is not correct.
                  Like i said --> the way that he includes the file is perfectly fine. And the included file (the original site.php) is not returning any HTML at all. It is returning
                  $online = 1

                  If he changes the code of site.php (the initial code from his first post) to
                  PHP Code:
                  <?php
                  echo ('<?php $online = 1; ?>');
                  ?>
                  then his initial code would work.

                  you can read more about including remote files on http://www.php.net/manual/en/function.include.php where it clearly states:
                  Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code.
                  and
                  Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
                  so that he uses the http wrapper doesn't mean that he gets html returned at all. You will get the output of the processed php, after the remove server processed that. But that can be php, html, xml, javascript, ...
                  think of it as this:
                  when you include a local file, the content of that local file will be copy and pasted into the including script --> but even in this case, you need to include the php tags in the included file because the server drops out of php mode when it includes the file.
                  when you include a relmote file, then the file is processed, copy and pasted --> so your output from the remote file should also be wrapped in php-tags and then it just as well becomes part of the including script.

                  Allsortshop,
                  i don't understand how your code could ever work. I mean, in the last code that you posted, you still do something absolutely pointless like
                  PHP Code:
                  $online 0;
                  if (
                  $online == 0) { 
                  this will always evaluate to True so what's the point it will always say that the site is ofline??

                  and you can not put the offline message inside site.php because when site.php is indeed ofline, you wount be able to parse and echo the content of it!

                  your code for index.php should be
                  PHP Code:
                  <?php
                  $online 
                  0;
                  include 
                  "http://www.allsortshop.com/site.php";
                  if (
                  $online == 0) { 
                      
                  header('Location: ./offline.php'); 
                      die(); 
                  }
                  ?>
                  <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
                  <html xmlns='http://www.w3.org/1999/xhtml'>
                  <head>
                  <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
                  <title>Allsortshop Coming Soon</title>
                  </head>
                  <body>
                  <div align='center'><img src='temp.png' /></div>
                  </body>
                  </html>
                  and the code for offline.php should be
                  PHP Code:
                  <?php
                  $online 
                  0;
                  include 
                  "http://www.allsortshop.com/site.php";
                  if (
                  $online == 1) { 
                      
                  header('Location: ./index.php'); 
                      die(); 
                  }
                  ?>
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html xmlns="http://www.w3.org/1999/xhtml">
                  <head>
                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                  <title>Allsortshop is Offline</title>
                  </head>
                  <body>
                  ALLSORTSHOP IS CURRENTLY OFFLINE. CHECK BACK SOON! 
                  </body>
                  </html>
                  and the code for site.php should be
                  PHP Code:
                  <?php
                  echo '<?php $online = 1; ?>';
                  ?>
                  i've set similar files on a testaccount of mine and they work.
                  the only extra requirement is that you have error reporting turned off, because when the server fails to include the remote file, it will issue a warning and that will cause the redirect to offline.php to fail.
                  Last edited by raf; Oct 13, 2006, 04:42 PM. Reason: part of first quote from php.net got lost
                  Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

                  Comment


                  • #10
                    Ok, thanks anyway,

                    But its all working so thats the main thing.

                    Also, i am not that good a php.

                    I will get better lol.

                    @Andy92 / Dribbble

                    Comment

                    Working...
                    X