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

change URL variables

P: n/a
Hi out there,
I have an url that looks like this: http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.

Is there any way to do so?

Thx.

Feb 28 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ab*****@gmail.com wrote:
Hi out there,
I have an url that looks like this: http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.

Is there any way to do so?
Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?

http://nl3.php.net/manual/en/reserve...riables.server
Feb 28 '07 #2

P: n/a
On Feb 28, 8:57 am, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
absc...@gmail.com wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.
Is there any way to do so?

Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?

http://nl3.php.net/manual/en/reserve...erved.variable...
Thx.
I tried the REQUEST_URI but didn't work. I'll look at your link and
update when I go back to my project. Thx.

Feb 28 '07 #3

P: n/a

// this will replace (and/or add) parameters to the query string
$new_url = get_self_link(array('var'=>'new_value'));
// may seem like a lot of code, but I use these 2 funcs all the time.

function get_self_link($new_get_vars=array(),$full_url=fals e)
{
$string = '';
$get_vars = $_GET;
foreach ( $new_get_vars as $k=>$v )
$get_vars[$k] = $v;
$protocol = ( isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']=='on' ) ? 'https' : 'http';
$current_url = $protocol.'://'.$_SERVER['HTTP_HOST'].
$_SERVER['REQUEST_URI'];
$url_parts = @parse_url($current_url);
$query = get_query_string($get_vars);
if ( $full_url )
$string = $protocol.'://'.$_SERVER['HTTP_HOST'];
$string .= $url_parts['path'].$query;
return $string;
}

/************************************************** **********/
// similar to http://www.php.net/function.http-build-query
// does not handle arrays
function get_query_string($attribs)
{
$query = '?';
foreach ( $attribs as $k=>$v )
{
if ( $v !== null && !is_array($v) )
{
$query .= $k;
$query .= '='.str_replace('%2F','/',urlencode($v));
$query .= '&';
}
}
$query = substr($query,0,-1);
$query = str_replace('&','&amp;',$query);
return $query;
}

On Feb 28, 7:40 am, absc...@gmail.com wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.

Is there any way to do so?

Thx.

Feb 28 '07 #4

P: n/a
On Feb 28, 8:57 am, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
absc...@gmail.com wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.
Is there any way to do so?

Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?

http://nl3.php.net/manual/en/reserve...erved.variable...
This is the error I get when using REQUEST_URI: Notice: Undefined
index: REQUEST_URI
Feb 28 '07 #5

P: n/a
ab*****@gmail.com wrote:
On Feb 28, 8:57 am, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
>absc...@gmail.com wrote:
>>Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.
Is there any way to do so?
Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?

http://nl3.php.net/manual/en/reserve...erved.variable...

This is the error I get when using REQUEST_URI: Notice: Undefined
index: REQUEST_URI

It needs to be:

$_SERVER['REQUEST_URI']

Note the quotes - this is an index in the $_SERVER array.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 1 '07 #6

P: n/a
On Mar 1, 12:09 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
absc...@gmail.com wrote:
On Feb 28, 8:57 am, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
absc...@gmail.com wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.
Is there any way to do so?
Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?
>http://nl3.php.net/manual/en/reserve...erved.variable...
This is the error I get when using REQUEST_URI: Notice: Undefined
index: REQUEST_URI

It needs to be:

$_SERVER['REQUEST_URI']

Note the quotes - this is an index in the $_SERVER array.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
My exact call are:
$url = $_SERVER['REQUEST_URI'];
echo "URI: ".$url."<br/>";

and still: Notice: Undefined index: REQUEST_URI in index.php on line
21
Mar 1 '07 #7

P: n/a
ab*****@gmail.com wrote:
On Mar 1, 12:09 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>absc...@gmail.com wrote:
>>On Feb 28, 8:57 am, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
absc...@gmail.com wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.
Is there any way to do so?
Yes. What do you have so far? Have you looked at $_SERVER[QUERY_STRING]
and $_SERVER[REQUEST_URI]?
http://nl3.php.net/manual/en/reserve...erved.variable...
This is the error I get when using REQUEST_URI: Notice: Undefined
index: REQUEST_URI
It needs to be:

$_SERVER['REQUEST_URI']

Note the quotes - this is an index in the $_SERVER array.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

My exact call are:
$url = $_SERVER['REQUEST_URI'];
echo "URI: ".$url."<br/>";

and still: Notice: Undefined index: REQUEST_URI in index.php on line
21

Well, it should work, if your server is passing it on to PHP. Which
server are you using?

What does phpinfo() show? It should show REQUEST_URI.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 1 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.