473,507 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jul 20 '05 #1
7 15901
"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
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
"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8237
by: alexz | last post by:
I'm redirecting a value to a page called update.asp from somepage.asp Recid = Request.QueryString("qryCategory") Response.redirect "update.asp?" & Recid So it shows like /update.asp?3 ...
5
2052
by: Lukelrc | last post by:
Hi, I have a dynamically created listbox. I'm trying to get one of the options selected according to a passed value. This is what i have: <select name="txtTheme" id="txtTheme"> ...
2
2547
by: Dennis M. Straussfogel | last post by:
I'm trying to implement a nested SSI file that has in it the line: <!--#include virtual="<!--#echo var='VAR_location'-->pagelinks.txt" --> where VAR_location is a variable set in the "calling"...
7
2463
by: Adam | last post by:
I've cobbled a sort of global "debug" routine by using this function: function debug($str, $strname) { global $debugYN; if ($debugYN) { echo "<br><br>"; } } This function gets included...
0
979
by: Mike Ratliff | last post by:
I need to call internet explorer from another program(or say from the run command) and pass along a variable to the aspx page that I am opening. example: iexplorer.exe search.aspx?variable1=12345...
1
1393
by: maheshd | last post by:
Hai I had the problem of using the variable in asp code that had passed in java script function Here the code i used function fun(form2,cname) { alert(cnme.value) <% set...
7
20743
by: malineau | last post by:
I would like to be able to open a link utilising a variable as the destination. Something like <a href=variablename><\a> where variablename specifies a page address within the website. But I can't...
8
3042
by: holy moly | last post by:
Hello, I am trying to make a PHP/HTML update script for MySQL. I prompt the user to enter the ID# of the record they wish to update into a form which then uses the passed ID# to select...
19
2107
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
1
2363
by: backups2007 | last post by:
What I need to do is be able to pass a variable that gets assigned as a selected value to a dorp down box. I need to pass it to another page. Please help.
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.