how do i get the current page url? anyweb page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chikooo
Forum Newbie
Posts: 12
Joined: Sun Feb 01, 2009 8:25 pm

how do i get the current page url? anyweb page

Post by chikooo »

ok so i have this web site where i would like to put pictures like the ones that appears on the sites that offer image comments for myspace, where the html code is below the image; the code where you can copy it and paste it.

so how do i make my web page to generate the code itself so i dont have to be writing the code on every page

i only know this php code that will write the domain name <?php echo $HTTP_SERVER_VARS['HTTP_HOST']; ?> but i want the full page url, and also if i can get the image full url.

anybody gets what im saying?
well the webpage is http://www.espan-glish.com/comments/
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: how do i get the current page url? anyweb page

Post by susrisha »

have you tried $_SERVER[PHP_SELF] ??
chikooo
Forum Newbie
Posts: 12
Joined: Sun Feb 01, 2009 8:25 pm

Re: how do i get the current page url? anyweb page

Post by chikooo »

i tried the code u gave me and i modified it to this <?php echo $_SERVER[PHP_SELF]; ?> and it gave me the page url just what i wanted. but now how do i get an image url
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: how do i get the current page url? anyweb page

Post by watson516 »

You know where the images are saved. You know what the image name is.

domain.com/imageFolder/imageName.ext
chikooo
Forum Newbie
Posts: 12
Joined: Sun Feb 01, 2009 8:25 pm

Re: how do i get the current page url? anyweb page

Post by chikooo »

so i found this webpage that has this


Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that.

Add the following code to a page:

<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
You can now get the current page URL using the line:

<?php
echo curPageURL();
?>
Sometimes it is needed to get the page name only. The following example shows how to do it:

<?php
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

echo "The current page name is ".curPageName();
?>




so i dont get where do i put the first code? it says on a page which page. i meAn should i put it on the page where i want the url to appear. on the header or where
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how do i get the current page url? anyweb page

Post by pickle »

Don't use $_SERVER['PHP_SELF'] - it isn't guaranteed to be set (comes from the client). Use $_SERVER['REQUEST_URI'] instead.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply