Jump to content


Photo

Current page URL in text box


  • Please log in to reply
4 replies to this topic

#1 thebluebus

thebluebus
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 11 September 2006 - 02:19 PM

Can anyone help. I want to add a text box to a page, which displays the current page URL. I've been told this can be done easily in php.

Anyone walk me through it?

thanks :)

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 September 2006 - 02:39 PM

<input name="url" type="text" value="<?php echo $_SERVER['REQUEST_URI']; ?>">


Should do the trick :)

Liam



EDIT: <input name="url" type="text" value="<?php echo $_SERVER['SERVER_NAME']; ?>">
will show just the domain...
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 11 September 2006 - 02:40 PM

There is probably a more simplier solution than this, but this is what a google search returned:

<?php

function selfURL() {
	$s = empty($_SERVER["HTTPS"]) ? ''
		: ($_SERVER["HTTPS"] == "on") ? "s"
		: "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
		: (":".$_SERVER["SERVER_PORT"]);
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}

print(selfURL());

?>

I have tested it and it works. It creates a function to retrieve the url. So to add that in a text box:

<?php

function selfURL() {
	$s = empty($_SERVER["HTTPS"]) ? ''
		: ($_SERVER["HTTPS"] == "on") ? "s"
		: "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? ""
		: (":".$_SERVER["SERVER_PORT"]);
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}
?>
<textarea><?php print(selfURL()); ?></textarea>



#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 11 September 2006 - 02:41 PM

shocker-z, I was going to suggest $_SERVER['REQUEST_URI'], but for some reason when I was testing it, it didn't display the full url? Maybe that was just my host.

#5 thebluebus

thebluebus
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 11 September 2006 - 02:42 PM

thanks both of you. i'll give them a try a bit later after work.  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users