redirect to URL passed as variable in php

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

    redirect to URL passed as variable in php


    I want to use javascript to redirect to a URL which has been passed
    as a variable (in php).

    I have searched but cannot find any solution.

    I think this code is a basic redirect:

    <script type="text/javascript">
    window.location = "http://www.someserver. com/somePage.html";
    </script>

    However, I need the URL in this code to be a variable.

    I would be grateful if anyone could tell me how to make this code
    handle a variable URL or just post the address where I can see some
    code which does this (I have searched for a while in google and in
    this newsgroup but cannot find anything).

    Thank You.
  • McKirahan

    #2
    Re: redirect to URL passed as variable in php

    "Donna Hawkins" <NOSPAM@hotmail .com> wrote in message
    news:3fe77db0.9 981582@News.CIS .DFN.DE...[color=blue]
    >
    > I want to use javascript to redirect to a URL which has been passed
    > as a variable (in php).
    >
    > I have searched but cannot find any solution.
    >
    > I think this code is a basic redirect:
    >
    > <script type="text/javascript">
    > window.location = "http://www.someserver. com/somePage.html";
    > </script>
    >
    > However, I need the URL in this code to be a variable.
    >
    > I would be grateful if anyone could tell me how to make this code
    > handle a variable URL or just post the address where I can see some
    > code which does this (I have searched for a while in google and in
    > this newsgroup but cannot find anything).
    >
    > Thank You.[/color]


    Perhaps; (watch for word-wrap),


    <script language="javas cript" type="text/javascript">
    function goto(url) {
    location.href = url;
    }
    </script>

    <a href="javascrip t:goto('http://www.someserver. com/somePage.html') "
    target="_blank" >somePage</a>


    Comment

    • Donna Hawkins

      #3
      Re: redirect to URL passed as variable in php

      On Tue, 23 Dec 2003 00:13:28 GMT, "McKirahan" <News@McKirahan .com>
      wrote:
      [color=blue]
      >"Donna Hawkins" <NOSPAM@hotmail .com> wrote in message
      >news:3fe77db0. 9981582@News.CI S.DFN.DE...[color=green]
      >>
      >> I want to use javascript to redirect to a URL which has been passed
      >> as a variable (in php).
      >>
      >> I have searched but cannot find any solution.
      >>
      >> I think this code is a basic redirect:
      >>
      >> <script type="text/javascript">
      >> window.location = "http://www.someserver. com/somePage.html";
      >> </script>
      >>
      >> However, I need the URL in this code to be a variable.
      >>
      >> I would be grateful if anyone could tell me how to make this code
      >> handle a variable URL or just post the address where I can see some
      >> code which does this (I have searched for a while in google and in
      >> this newsgroup but cannot find anything).
      >>
      >> Thank You.[/color]
      >
      >
      >Perhaps; (watch for word-wrap),
      >
      >
      ><script language="javas cript" type="text/javascript">
      >function goto(url) {
      > location.href = url;
      >}
      ></script>
      >
      ><a href="javascrip t:goto('http://www.someserver. com/somePage.html') "
      >target="_blank ">somePage</a>[/color]

      Thanks McKirahan

      I can't see how the url is variable when you have an actual url inside
      the javascript.

      But the main problem is ... you have presented a clickable link when
      what I require is an automatic redirect.

      I am not sure if the code I wrote in my first post code is for a
      redirect or not ... it was the best I could find in my search.

      Comment

      • McKirahan

        #4
        Re: redirect to URL passed as variable in php

        "Donna Hawkins" <NOSPAM@hotmail .com> wrote in message
        news:3fe7934a.1 5511714@News.CI S.DFN.DE...[color=blue]
        > On Tue, 23 Dec 2003 00:13:28 GMT, "McKirahan" <News@McKirahan .com>
        > wrote:
        >[color=green]
        > >"Donna Hawkins" <NOSPAM@hotmail .com> wrote in message
        > >news:3fe77db0. 9981582@News.CI S.DFN.DE...[color=darkred]
        > >>
        > >> I want to use javascript to redirect to a URL which has been passed
        > >> as a variable (in php).
        > >>
        > >> I have searched but cannot find any solution.
        > >>
        > >> I think this code is a basic redirect:
        > >>
        > >> <script type="text/javascript">
        > >> window.location = "http://www.someserver. com/somePage.html";
        > >> </script>
        > >>
        > >> However, I need the URL in this code to be a variable.
        > >>
        > >> I would be grateful if anyone could tell me how to make this code
        > >> handle a variable URL or just post the address where I can see some
        > >> code which does this (I have searched for a while in google and in
        > >> this newsgroup but cannot find anything).
        > >>
        > >> Thank You.[/color]
        > >
        > >
        > >Perhaps; (watch for word-wrap),
        > >
        > >
        > ><script language="javas cript" type="text/javascript">
        > >function goto(url) {
        > > location.href = url;
        > >}
        > ></script>
        > >
        > ><a href="javascrip t:goto('http://www.someserver. com/somePage.html') "
        > >target="_blank ">somePage</a>[/color]
        >
        > Thanks McKirahan
        >
        > I can't see how the url is variable when you have an actual url inside
        > the javascript.
        >
        > But the main problem is ... you have presented a clickable link when
        > what I require is an automatic redirect.
        >
        > I am not sure if the code I wrote in my first post code is for a
        > redirect or not ... it was the best I could find in my search.[/color]

        You stated: "... I need the URL in this code to be a variable" --

        The JavaScript function accepts "url" (a variable); whereas your example had
        it hardcoded.

        I presented a clickable link as a way to show how to pass a URL to the
        function.



        But I guess the real problem is that I do not understand what you're trying
        to do.

        Could you explain your problem (and your efforts) in a different (more
        concise) manner?


        Comment

        • Richard Cornford

          #5
          Re: redirect to URL passed as variable in php

          "Donna Hawkins" <NOSPAM@hotmail .com> wrote in message
          news:3fe77db0.9 981582@News.CIS .DFN.DE...[color=blue]
          >I want to use javascript to redirect to a URL which has
          >been passed as a variable (in php).
          >
          >I have searched but cannot find any solution.
          >
          >I think this code is a basic redirect:
          >
          > <script type="text/javascript">
          > window.location = "http://www.someserver. com/somePage.html";
          > </script>[/color]

          That is a redirect, in the sense that it will navigate the window/frame
          to the URL assigned to the location object. It will only work if the
          client has JavaScript enabled (and sometimes not even then).

          HTTP has its own redirect method and server-side scripting languages
          should provide a mechanism to use it directly. I have a limited
          familiarity with PHP but in JSP the command:-

          response.sendRe direct("http://example.com/examplePage.htm l");

          - can be used to (HTTP) redirect the browser. I would be very surprised
          if PHP did not provide a similar ability, and using HTTP to redirect
          avoids the dependence on client-side scripting.

          It is also possible to forward on the server so the client gets what it
          thinks is a response to its original request but that response is
          generated by a different resource to the one that the original request
          was addressed to.
          [color=blue]
          >However, I need the URL in this code to be a variable.
          >
          >I would be grateful if anyone could tell me how to make
          >this code handle a variable URL ...[/color]
          <snip>

          You have not been clear about where the variable is. Remember the
          separation between PHP which executes on the server and generates a
          response for the browsers and the JavaScript code included in (or
          referenced by) that response, which is executed on the client. Variables
          in PHP are in a different environment to JavaScript variables and they
          have no direct access to each other.

          To assign a JavaScript variable to the location object is as simple as
          using the identifier for the variable where the string literal of the
          URL appears in the script above.

          A PHP variable can effectively be made available to the client-side
          script included in the response by writing its value into the generated
          JavaScript source code in the same way as the value of a PHP variable
          can be written into the generated HTML. I don’t know the PHP equivalent
          to the JSP/ASP expression tag (<%= [expression] %>) but there will be
          one (and I believe ASP style scriptlet tags can be made available in
          PHP). Inserting the value of a server-side Java (String) variable
          (called "variableNa me" in this example) into JavaScript source code
          generated in a JSP would be:-

          <script type="text/javascript">
          location = "<%= variableName %>";
          </script>

          Note that in order for the value of the variable to be treated as a
          string literal in the resulting JavaScript source code the expression
          tag is surrounded with quote marks. It is also important to consider
          what the value being written into the response may contain, double
          quotes and carriage returns would render the resulting JavaScript source
          code syntactically incorrect and some characters may need to be escaped
          when used in a string literal. That shouldn't be a problem if the string
          is a URL.

          Richard.


          Comment

          • HikksNotAtHome

            #6
            Re: redirect to URL passed as variable in php

            In article <bs8gtj$5r5$1$8 300dec7@news.de mon.co.uk>, "Richard Cornford"
            <Richard@litote s.demon.co.uk> writes:
            [color=blue]
            >HTTP has its own redirect method and server-side scripting languages
            >should provide a mechanism to use it directly. I have a limited
            >familiarity with PHP but in JSP the command:-
            >
            >response.sendR edirect("http://example.com/examplePage.htm l");
            >
            >- can be used to (HTTP) redirect the browser. I would be very surprised
            >if PHP did not provide a similar ability, and using HTTP to redirect
            >avoids the dependence on client-side scripting.[/color]

            In PHP, you use the Location Header to redirect.

            header("Locatio n: http://www.example.com/examplePage.htm l");
            --
            Randy

            Comment

            • Donna Hawkins

              #7
              Re: redirect to URL passed as variable in php

              On Mon, 22 Dec 2003 23:32:02 GMT, NOSPAM@hotmail. com (Donna Hawkins)
              wrote:
              [color=blue]
              >
              >I want to use javascript to redirect to a URL which has been passed
              >as a variable (in php).
              >
              >I have searched but cannot find any solution.
              >
              >I think this code is a basic redirect:
              >
              ><script type="text/javascript">
              >window.locatio n = "http://www.someserver. com/somePage.html";
              ></script>
              >
              >However, I need the URL in this code to be a variable.
              >
              >I would be grateful if anyone could tell me how to make this code
              >handle a variable URL or just post the address where I can see some
              >code which does this (I have searched for a while in google and in
              >this newsgroup but cannot find anything).[/color]

              Thanks McKirahan, Richard, and Hikks

              I apologise for not explaining it properly.

              The user clicks on a link and then this link goes to a redirect page
              with the URL passed as a variable such as:



              I then want to record in a mysql database that there has been a click
              for this 'www.variableur l.com'

              Then I want to redirect to the variable "url".

              Hikks is quite right about the php redirect:
              -------------------------------------------------------------------------------------------------
              //give the variable $redirectURL the value of the passed "url" (i know
              you can just use '$url' but just showing what is happening)

              $redirectURL=$u rl

              // then redirect to the variable

              header("Locatio n:$redirectURL" );
              --------------------------------------------------------------------------------------------------
              I wanted to know the javascript for a few reasons such as:

              1/ whether javascript stamps the click as coming from 'redirect.php'
              (the php redirect stamps it with the page that the user originally
              clicked on and not the redirecting page)

              2/ whether it can be used to hide the links from google

              3/ whether the javascript redirect can be used after there has been
              output (php redirect can only be used if there has been no output).

              I didn't think the javascript would be that difficult and, as I said,
              I searched for a while for a solution but there was none there.

              Anyway thanks again, I learnt something just by studying your answers.

              Comment

              • HikksNotAtHome

                #8
                Re: redirect to URL passed as variable in php

                In article <3fe81e2c.31408 13@News.CIS.DFN .DE>, NOSPAM@hotmail. com (Donna
                Hawkins) writes:
                [color=blue]
                >
                >Thanks McKirahan, Richard, and Hikks
                >
                >I apologise for not explaining it properly.
                >
                >The user clicks on a link and then this link goes to a redirect page
                >with the URL passed as a variable such as:
                >
                >http://www.mydomain.com/redirect.php...ariableurl.com
                >
                >I then want to record in a mysql database that there has been a click
                >for this 'www.variableur l.com'
                >
                >Then I want to redirect to the variable "url".
                >
                >Hikks is quite right about the php redirect:
                >
                >-------------------------------------------------------------------------[/color]
                ------------------------[color=blue]
                >//give the variable $redirectURL the value of the passed "url" (i know
                >you can just use '$url' but just showing what is happening)
                >
                >$redirectURL=$ url
                >
                >// then redirect to the variable
                >
                > header("Locatio n:$redirectURL" );
                >
                >-------------------------------------------------------------------------[/color]
                -------------------------[color=blue]
                >I wanted to know the javascript for a few reasons such as:
                >
                >1/ whether javascript stamps the click as coming from 'redirect.php'
                >(the php redirect stamps it with the page that the user originally
                >clicked on and not the redirecting page)[/color]

                alert(document. referrer)

                in the final page might tell you. document.referr er only holds a value if the
                page was reached via a link from another page. A simple test should be able to
                tell you. I don't have PHP running at the moment to make the test pages to find
                out :(
                [color=blue]
                >2/ whether it can be used to hide the links from google[/color]

                I think thats the first time I have read about someone wanting to hide links
                from google, its usually the other way around :)
                [color=blue]
                >3/ whether the javascript redirect can be used after there has been
                >output (php redirect can only be used if there has been no output).[/color]

                No output to the browser, as you know. It can process for days (if need be) as
                long as it doesn't output to the browser. Meaning, before you send the location
                header, have PHP log the "hit".

                <?php

                //code here to put the entry in the data basee

                header(location .........);
                [color=blue]
                >I didn't think the javascript would be that difficult and, as I said,
                >I searched for a while for a solution but there was none there.[/color]

                The closest javascript will have for a header(location redirect would be
                location.replac e which replaces the current page in the history with the new
                page. Thus when the back button is clicked, they go back to the first page, and
                skip the redirect page. Clicking the Forward button from there takes them back
                to the redirected page, not the one doing the redirecting.
                [color=blue]
                >Anyway thanks again, I learnt something just by studying your answers.[/color]

                Hope this helps.
                --
                Randy

                Comment

                Working...