How to get the full URL?

Dear Experts,

How do you get the current url and echo it onto the screen?

I have tried $_SERVER['PHP_SELF'] but it gives me back the actual url instead of the url written in the address bar.

Please help.
LVL 1
narmi2Asked:
Who is Participating?
 
Loganathan NatarajanConnect With a Mentor LAMP DeveloperCommented:
you can also try,


<?php
$full_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $full_url;
?>

Open in new window

0
 
hieloCommented:
Try:

echo $_SERVER['PHP_SELF'] . ( !empty($_SERVER['QUERY_STRING']) ? "?" . $_SERVER['QUERY_STRING'] : "";
0
 
Loganathan NatarajanLAMP DeveloperCommented:
also,
$url = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
narmi2Author Commented:
What I mean is, php_self returns something like /folder/filename.php

I am using .htaccess to rewrite the url to something like

/folder/filename-p3.php

How do I get the rewritten url, i.e. the url which is displayed in the address bar?
0
 
bevhostCommented:
You may also want
        if ( isset($HTTPS) && $HTTPS == 'on' ) {
          $PROTOCOL='https';
        } else {
          $PROTOCOL='http';
        }
        echo $PROTOCOL. "://".$_SERVER["HTTP_HOST"];
0
 
Loganathan NatarajanLAMP DeveloperCommented:
dear narmi2

did you try this?
<?php
$url = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo $url;
?>

Open in new window

0
 
narmi2Author Commented:
REQUEST_URI did the trick.  Thanks
0
 
Loganathan NatarajanLAMP DeveloperCommented:
Coool!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.