By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

redirect to URL passed as variable in php

P: n/a

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.
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f**************@News.CIS.DFN.DE...

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.

Perhaps; (watch for word-wrap),
<script language="javascript" type="text/javascript">
function goto(url) {
location.href = url;
}
</script>

<a href="javascript:goto('http://www.someserver.com/somePage.html')"
target="_blank">somePage</a>
Jul 20 '05 #2

P: n/a
On Tue, 23 Dec 2003 00:13:28 GMT, "McKirahan" <Ne**@McKirahan.com>
wrote:
"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f**************@News.CIS.DFN.DE...

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.

Perhaps; (watch for word-wrap),
<script language="javascript" type="text/javascript">
function goto(url) {
location.href = url;
}
</script>

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


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.
Jul 20 '05 #3

P: n/a
"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f***************@News.CIS.DFN.DE...
On Tue, 23 Dec 2003 00:13:28 GMT, "McKirahan" <Ne**@McKirahan.com>
wrote:
"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f**************@News.CIS.DFN.DE...

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.

Perhaps; (watch for word-wrap),
<script language="javascript" type="text/javascript">
function goto(url) {
location.href = url;
}
</script>

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


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.


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?
Jul 20 '05 #4

P: n/a
"Donna Hawkins" <NO****@hotmail.com> wrote in message
news:3f**************@News.CIS.DFN.DE...
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>
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.sendRedirect("http://example.com/examplePage.html");

- 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.
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 ...

<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 "variableName" 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.
Jul 20 '05 #5

P: n/a
In article <bs*******************@news.demon.co.uk>, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> writes:
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.sendRedirect("http://example.com/examplePage.html");

- 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.


In PHP, you use the Location Header to redirect.

header("Location: http://www.example.com/examplePage.html");
--
Randy
Jul 20 '05 #6

P: n/a
On Mon, 22 Dec 2003 23:32:02 GMT, NO****@hotmail.com (Donna Hawkins)
wrote:

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).


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.variableurl.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=$url

// then redirect to the variable

header("Location:$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.

Jul 20 '05 #7

P: n/a
In article <3f**************@News.CIS.DFN.DE>, NO****@hotmail.com (Donna
Hawkins) writes:

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.variableurl.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=$url

// then redirect to the variable

header("Location:$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)
alert(document.referrer)

in the final page might tell you. document.referrer 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 :(
2/ whether it can be used to hide the links from google
I think thats the first time I have read about someone wanting to hide links
from google, its usually the other way around :)
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).
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.........);
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.
The closest javascript will have for a header(location redirect would be
location.replace 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.
Anyway thanks again, I learnt something just by studying your answers.


Hope this helps.
--
Randy
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.