Jump to content

Archived

This topic is now archived and is closed to further replies.

thebluebus

Current page URL in text box

Recommended Posts

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

Share this post


Link to post
Share on other sites
<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...

Share this post


Link to post
Share on other sites
There is probably a more simplier solution than this, but this is what a google search returned:

[code]
<?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());

?>
[/code]

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

[code]
<?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>

[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.