PHP Redirect

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    PHP Redirect

    Hello,


    I have a php contact script and I want it to redirect to my homepage
    without using the standard header command.


    <?php
    header("locatio n:http://www.johndoe.com/index.html");
    exit;
    ?>




    Does anyone know a way to do this?
  • Arjen

    #2
    Re: PHP Redirect

    John schreef:
    Hello,
    >
    I have a php contact script and I want it to redirect to my homepage
    without using the standard header command.
    >
    <?php
    header("locatio n:http://www.johndoe.com/index.html");
    exit;
    ?>
    >
    Does anyone know a way to do this?
    Use meta refresh
    <meta http-equiv="refresh" content="0;URL= http://www.url.ext/" />

    Aldough it makes that nasty double clicking noise on some systems.

    --
    Arjen
    HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

    Comment

    • Lixas

      #3
      Re: PHP Redirect

      Hi John, I have php function that can be used for users browser
      redirection:
      <?
      function redirect($locat ion, $type="header")
      {
      if ($type == "header")
      {
      header("Locatio n: ".$location );
      exit;
      }
      elseif($type == "script")
      {
      echo "<script
      type='text/javascript'>doc ument.location. href='".$locati on."'</script>\n";
      die("redirectin g: please <a href=\"$locatio n\">click here</aif you
      are not redirected in 5 sec.");
      }
      elseif($type == "meta")
      {
      die("
      <html><head><ti tle>Redirecting ...</title>
      <META HTTP-EQUIV=\"Refresh \" CONTENT=\"3; URL=$location\" >
      </head>
      <body>redirecti ng: please <a href=\"$locatio n\">click here</aif you
      are not redirected in 5 sec</body>
      </html>");
      }
      }

      // usage
      redirect("www.y ourserver.com/yourpage"); //this will use php header
      type of redirection

      redirect("www.y ourserver.com/yourpage", "script") //this will create
      javascript with redirections instruction for user and link in case of
      JS is turned off

      redirect("www.y ourserver.com/yourpage", "meta") //this will redirect
      your user to your page using meta tags and also creates link to
      destination page in case of some kind of error
      ?>

      I think that is all types of redirecting user to other page

      Comment

      • John

        #4
        Re: PHP Redirect

        On Mon, 15 Jan 2007 19:38:57 +0100, Arjen <dont@mail.mewr ote:
        >John schreef:
        >Hello,
        >>
        >I have a php contact script and I want it to redirect to my homepage
        >without using the standard header command.
        >>
        ><?php
        >header("locati on:http://www.johndoe.com/index.html");
        >exit;
        >?>
        >>
        >Does anyone know a way to do this?
        >
        >Use meta refresh
        ><meta http-equiv="refresh" content="0;URL= http://www.url.ext/" />
        >
        >Aldough it makes that nasty double clicking noise on some systems.



        Thank you sir - that worked perfect.

        Comment

        • Michael Fesser

          #5
          Re: PHP Redirect

          ..oO(John)
          >I have a php contact script and I want it to redirect to my homepage
          >without using the standard header command.
          >
          >
          ><?php
          >header("locati on:http://www.johndoe.com/index.html");
          >exit;
          >?>
          That's the correct way. What's wrong with it?

          Micha

          Comment

          • Michael Fesser

            #6
            Re: PHP Redirect

            ..oO(Arjen)
            >Use meta refresh
            ><meta http-equiv="refresh" content="0;URL= http://www.url.ext/" />
            >
            >Aldough it makes that nasty double clicking noise on some systems.
            And breaks the back button ...

            Micha

            Comment

            • Arjen

              #7
              Re: PHP Redirect

              Michael Fesser schreef:
              .oO(John)
              >
              >I have a php contact script and I want it to redirect to my homepage
              >without using the standard header command.
              >>
              >>
              ><?php
              >header("locati on:http://www.johndoe.com/index.html");
              >exit;
              >?>
              >
              That's the correct way. What's wrong with it?
              >
              Micha
              I'm guessing headers allready sent error.

              If op could show some code it would be clear :-)

              --
              Arjen
              http://www.hondenpage.com - Mijn site over honden

              Comment

              • Petr Vileta

                #8
                Re: PHP Redirect

                "Arjen" <dont@mail.mepí se v diskusním príspevku
                news:eohv3v$nbl $1@brutus.eur.n l...
                Michael Fesser schreef:
                >.oO(John)
                >>
                >>I have a php contact script and I want it to redirect to my homepage
                >>without using the standard header command.
                >>>
                >>>
                >><?php
                >>header("locat ion:http://www.johndoe.com/index.html");
                >>exit;
                >>?>
                >>
                >That's the correct way. What's wrong with it?
                >>
                >Micha
                >
                I'm guessing headers allready sent error.
                >
                If op could show some code it would be clear :-)
                >
                Because header must be send BEFORE anything other. Sample code

                <?php
                if( some condition) {
                header("Locatio n: www.google.com" ); }
                else {
                // read some from database }
                // read or set session
                ?>
                <html><head>
                .....

                --

                Petr Vileta, Czech republic
                (My server rejects all messages from Yahoo and Hotmail. Send me your mail
                from another non-spammer site please.)



                Comment

                • Michael Fesser

                  #9
                  Re: PHP Redirect

                  ..oO(Arjen)
                  >Michael Fesser schreef:
                  >.oO(John)
                  >>
                  >><?php
                  >>header("locat ion:http://www.johndoe.com/index.html");
                  >>exit;
                  >>?>
                  >>
                  >That's the correct way. What's wrong with it?
                  >>
                  >I'm guessing headers allready sent error.
                  Then it would be time to fix the script instead of using ugly and
                  unreliable client-side redirects.
                  >If op could show some code it would be clear :-)
                  Yep.

                  Micha

                  Comment

                  • John

                    #10
                    Re: PHP Redirect

                    On Tue, 16 Jan 2007 08:33:18 +0100, Arjen <dont@mail.mewr ote:
                    >Michael Fesser schreef:
                    >.oO(John)
                    >>
                    >>I have a php contact script and I want it to redirect to my homepage
                    >>without using the standard header command.
                    >>>
                    >>>
                    >><?php
                    >>header("locat ion:http://www.johndoe.com/index.html");
                    >>exit;
                    >>?>
                    >>
                    >That's the correct way. What's wrong with it?
                    >>
                    >Micha
                    >
                    >I'm guessing headers allready sent error.
                    >
                    >If op could show some code it would be clear :-)


                    Here is the code:



                    <?php

                    // Define your email address - where to send messages - here
                    define("MAIL_TA RGET","johndoe@ nowhere.com");

                    // Here you can redefine error messages
                    define("errorNa me","Invalid name! It must be at least 2 characters
                    long");
                    define("errorEm ail","Invalid email address!");
                    define("errorMs g","Invalid message! It must be at least 10 characters
                    long");

                    function
                    createForm($nam e="",$email="", $message="",$er ror1="",$error2 ="",$error3="") {
                    ?>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
                    method="post">
                    <table>
                    <tr><td>Name: </td><td class="error">< ?php echo $error1;
                    ?></td></tr>
                    <tr><td colspan="2"><in put class="text" type="text"
                    name="name" value="<?php echo $name; ?>"></td></tr>
                    <tr><td>Email :</td><td class="error">< ?php echo $error2;
                    ?></td></tr>
                    <tr><td colspan="2"><in put class="text" type="text"
                    name="email" value="<?php echo $email; ?>"></td></tr>
                    <tr><td>Message :</td><td class="error">< ?php echo $error3;
                    ?></td></tr>
                    <tr><td colspan="2"><te xtarea cols="40" rows="6"
                    name="message"> <?php echo $message; ?></textarea></td></tr>
                    <tr><td colspan="2"><br/><input class="text" type="submit"
                    name="submitBtn " value="Send"></td></tr>
                    </table>
                    </form>
                    <?php
                    }

                    function isValidEmail($e mail){
                    $pattern =
                    "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

                    if (eregi($pattern , $email)){
                    return true;
                    }
                    else {
                    return false;
                    }
                    }

                    function sendMail($name, $email,$message ){

                    $subject = "Message from website";
                    $from = "From: $name <$email>\r\nRep ly-To: $email\r\n";
                    $header = "MIME-Version: 1.0\r\n"."Conte nt-type: text/html;
                    charset=iso-8859-1\r\n";
                    $content = htmlspecialchar s($message);

                    $content = wordwrap($conte nt,70);
                    mail(MAIL_TARGE T,$subject,$con tent,$from.$hea der);

                    }
                    ?>

                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                    "DTD/xhtml1-transitional.dt d">
                    <html>
                    <head>
                    <title>Contac t Form</title>
                    <link href="style/style.css" rel="stylesheet " type="text/css" />
                    </head>
                    <body>
                    <div id="main">
                    <div id="caption">Co ntact us</div>
                    <div id="icon">&nbsp ;</div>
                    <?php if (!isset($_POST['submitBtn'])) {
                    createForm();
                    } else {
                    $name = isset($_POST['name']) ? $_POST['name'] : "";
                    $email = isset($_POST['email']) ? $_POST['email'] : "";
                    $message = isset($_POST['message']) ? $_POST['message'] : "";

                    $error = false;

                    if (strlen($name)< 2) {
                    $error = true;
                    $error1 = errorName;
                    }
                    if (!isValidEmail( $email)) {
                    $error = true;
                    $error2 = errorEmail;
                    }
                    if (strlen($messag e)<10) {
                    $error = true;
                    $error3 = errorMsg;
                    }

                    if ($error){
                    createForm($nam e,$email,$messa ge,$error1,$err or2,$error3);
                    }
                    else {
                    sendMail($name, $email,$message );
                    ?>

                    <div id="result">
                    <table width="100%">
                    <tr><td>
                    Thanks for your message!
                    </td></tr>
                    </table>
                    </div>


                    <?php
                    }
                    }
                    ?>
                    <div>
                    </body>

                    Comment

                    • John Dunlop

                      #11
                      Re: PHP Redirect

                      Arjen:
                      Use meta refresh
                      <meta http-equiv="refresh" content="0;URL= http://www.url.ext/" />
                      The meta refresh can be bewildering, all the more so if
                      the user isn't looking at the page when it "redirects" . It
                      can knock the user's mental model out of kilter.

                      And, as Michael pointed out, it can also break the Back
                      button, because as soon as you go Back to the page with the
                      meta refresh, it "redirects" you again. Hitting the Back
                      button twice with a delay of zero is tricky.

                      The meta refresh also doesn't offer different types of
                      "redirect" - for example, permanent, temporary - but only one
                      fake redirect, lodged in the markup of the page.

                      And some browsers don't even support the meta refresh,
                      still others allow you to turn it off, so you would have to
                      offer another means of getting to the new page, such as a link.

                      What do you gain from the meta refresh and would it not be
                      better to either simply link to the new page or set up a real
                      redirect?

                      --
                      Jock

                      Comment

                      • Carl Pearson

                        #12
                        Re: PHP Redirect

                        John wrote:
                        Hello,
                        >
                        >
                        I have a php contact script and I want it to redirect to my homepage
                        without using the standard header command.
                        >
                        >
                        <?php
                        header("locatio n:http://www.johndoe.com/index.html");
                        exit;
                        ?>
                        >
                        >
                        >
                        >
                        Does anyone know a way to do this?
                        You could do it on the client via javascript, but that would mean an
                        additional call to the server (assuming the redirection is going to
                        somewhere else on the same site):

                        function Redirect($Url = "/")
                        {
                        echo "<script
                        language='JavaS cript'>document .location.href= '".$Url."'</script>";
                        exit;
                        }

                        Comment

                        • Arjen

                          #13
                          Re: PHP Redirect

                          John schreef:
                          <?php if (!isset($_POST['submitBtn'])) {
                          createForm();
                          } else {
                          <snip>
                          else {
                          sendMail($name, $email,$message );
                          Try something like this on top of ur page

                          if (!isset($_POST['submitBtn'])
                          {
                          // do ur mail check etc
                          sendMail($name, $email,$message );
                          header('Locatio n: www.example.com ');
                          exit;
                          }
                          else
                          {
                          //other stuff
                          }

                          <html>
                          etc

                          --
                          Arjen
                          HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

                          Comment

                          • dan@tobias.name

                            #14
                            Re: PHP Redirect

                            On Jan 16, 10:42 am, John <John_nos...@nn nnnnnnn.nowhere wrote:
                            }function isValidEmail($e mail){
                            $pattern =
                            "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
                            >
                            if (eregi($pattern , $email)){
                            return true;
                            }
                            else {
                            return false;
                            }
                            OK, this isn't the main topic of this thread, but I have to remark
                            about the above function which was posted as an example of the code
                            John is using; it appears that this requires as one aspect of a "valid"
                            e-mail address that it have a top-level domain that is exactly 2 or 3
                            characters long, which would exclude addresses in .info, .name,
                            ..museum, and some other TLDs.

                            --
                            Dan

                            Comment

                            • Rik

                              #15
                              Re: PHP Redirect

                              On Thu, 25 Jan 2007 22:15:43 +0100, <dan@tobias.nam ewrote:
                              On Jan 16, 10:42 am, John <John_nos...@nn nnnnnnn.nowhere wrote:
                              >}function isValidEmail($e mail){
                              > $pattern =
                              >"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
                              >>
                              > if (eregi($pattern , $email)){
                              > return true;
                              > }
                              > else {
                              > return false;
                              > }
                              >
                              OK, this isn't the main topic of this thread, but I have to remark
                              about the above function which was posted as an example of the code
                              John is using; it appears that this requires as one aspect of a "valid"
                              e-mail address that it have a top-level domain that is exactly 2 or 3
                              characters long, which would exclude addresses in .info, .name,
                              .museum, and some other TLDs.
                              Yup, I've given up trying to regex valid emaildresses (try to find
                              Friedl's total email validation regex, it's a terrible beast). There are
                              more characters allowed then just [a-z0-9_.-]. I'm just checking if the
                              adress contains one and only one @, and wether I can locate the domain
                              from the server.

                              --
                              Rik Wasmus
                              * I'm testing several new newsreaders at the moment. Please excuse
                              possible errors and weird content. *

                              Comment

                              Working...