Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current URL of my script using PHP? [duplicate]

Tags:

url

php

How do I get the current URL of my script using PHP?

like image 560
Steven Avatar asked Dec 09 '09 05:12

Steven


2 Answers

Please see PHP Get Current URL:

Sometimes it is not as straightforward as one may think to get the current url to use it inside your application. Here is a snippet that I use to fetch the current URL and use it in a script. The current url (whether http or https) is now a local variable that you can do with as you please.

$url = "http".(!empty($_SERVER['HTTPS'])?"s":"").
"://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Note: You should groom this value before using in anything sensitive, like a sql query.

like image 130
Andrew Hare Avatar answered Oct 06 '22 17:10

Andrew Hare


For www.example.com/files/script.php

$_SERVER['SCRIPT_NAME'] = /files/script.php

and if you want the current directory url:

dirname($_SERVER['SCRIPT_NAME']) = /files

like image 21
adrianTNT Avatar answered Oct 06 '22 18:10

adrianTNT