473,376 Members | 1,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

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 15885
"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
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
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
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
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
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
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
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
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
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
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.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.