novactown
novactown

Reputation: 137

PHP $_SERVER and urls?

How can I get my full current url in php but minus all querystrings?

Example

echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

Would echo something like assuming query strings were in place...

http://www.example.com/example?tab=foo&dslip=yes

How can I get the same as above but cut off all the query strings?

How is this done in php.

Thanks.

Upvotes: 1

Views: 424

Answers (3)

lynks
lynks

Reputation: 5689

PHP_SELF is what you need I believe.

echo('http' . ((empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']);

Or the __FILE__ constant, depending on your exact configuration and situation.

Upvotes: 5

Jorge Campos
Jorge Campos

Reputation: 23381

I think the solution is this:

echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];

the $_SERVER['SCRIPT_NAME'] variable returns path of your script without the query string like you want.

if you like to see all variables available in php just try this

phpinfo();

Best regards

Upvotes: 0

Vaibhav Naranje
Vaibhav Naranje

Reputation: 78

Try this

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

$part = explode('?',$url);

echo $part[0];

Upvotes: 1

Related Questions