5

Hello Friends I want to get the Current Page Url instead of capturing root url of file

I want to capture Like this http://localhost/tester/

but coming like this

http://localhost/tester/views/inc/readcountry.php

I have tried like this but it's returning

$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]

localhost/tester/views/inc/readcountry.php

6
  • 1
    DOCUMENT_ROOT may be the solution Commented May 31, 2016 at 6:30
  • 8
    Possible duplicate of How to get URL of current page in PHP Commented May 31, 2016 at 6:30
  • 2
    Possible duplicate of Get the full URL in PHP Commented May 31, 2016 at 6:31
  • @FrayneKonok not working it's returning local directory structure .. Commented May 31, 2016 at 6:33
  • 1
    You mean something like this: $path = explode("/",$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); echo "http://".$path[0]."/".$path[1];? Commented May 31, 2016 at 6:36

4 Answers 4

2

This one is working perfectly thanx alot

$_SERVER['QUERY_STRING']
Sign up to request clarification or add additional context in comments.

2 Comments

How this is your desire output???? compare I want to capture Like this localhost/SporTwitter and your result.
@FrayneKonok I want to capture like this localhost/SporTwitter instead of capturing files root url.
1

A little handy function I found somewhere else might help you out.

function url() {
  return sprintf(
    "%s://%s%s",
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
    $_SERVER['SERVER_NAME'],
    $_SERVER['REQUEST_URI']
  );
}

To use it:

echo url();

1 Comment

Thanx Peter I got the solution, this one i have tried $_SERVER['QUERY_STRING'] I Will also check your suggestion
1

if you want the full url, try this:

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

$_SERVER['HTTP_HOST'] - header from the current request, if there is one.

$_SERVER['REQUEST_URI'] - the URI which was given in order to access this page; for instance, '/index.html'.

$_SERVER['QUERY_STRING'] - it is your parameters and values in URL


My htaccess for MVC app:

RewriteEngine on

RewriteRule !.(js|gif|jpg|png|css)$ index.php

Plus in each directory (model, views, controllers) i putted htaccess too:

Deny from all

3 Comments

Thanx for Your reply, This one I have tried but returning like this localhost/SporTwitter/views/inc/readcountry.php but my requirement is like this localhost/SporTwitter
@Rahul In my MVC framework i used mod_rewrite in apache. It helped me.
Thanx I will keep your Suggestion in my Note.
0

Not knowing which is the point of the tree you need to stop the capture, I may suggest you to have a look at all the content of the [$_SERVER] array, dumping it, and at the php magic constants.

1 Comment

Thanx for Your suggestion i am going to check now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.