Putting URL in variable then Echo

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
iwmichael
Forum Newbie
Posts: 1
Joined: Sat Dec 31, 2011 6:36 pm

Putting URL in variable then Echo

Post by iwmichael »

I'm having trouble using PHP to display the current URL in the Address bar. I see I need to use $_SERVER['REQUEST_URI'] but I need it in a variable then echo it later on a page.

Code: Select all

<?php
if(!$id || count($headerinfo) < 1) { echo '<a href="'.$site_url.'">'.$site_name.'</a> / '.$pname.'';
$comments_layout = '1';
$comMents = "";

} else { 
$comments_layout = '2';
$comMents = "<div class="alignleft"> PHP to show Page URL </div>";
?>

HTML Code
<?php echo '$comMents'; ?>
I tried reading some tutorials but I'm not seeing a solution for doing this with PHP.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: Putting URL in variable then Echo

Post by Gopesh »

Hi,If u need to get the current url and to store it in a variable check

Code: Select all


$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
//This will echo the url
 echo $pageURL;
//echo with link
echo "<a href=$pageURL'>Url </a>";
Hope it helps u..
Post Reply