Shinu Thomas
Shinu Thomas

Reputation: 5316

How to get the full page url using php (without page name and parameters)

I am running a page with url like this http://www.domain.com/test/reports/index.php

I need to get the url using php with out index.php like

http://www.domain.com/test/reports/

Upvotes: 0

Views: 165

Answers (4)

Karan Punamiya
Karan Punamiya

Reputation: 8863

Use parse_url:

$url = (($_SERVER['HTTPS']=="on")?"https://":"http://").$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URL'];
$parts = parse_url($url);
$urlpath = $parts['scheme']."://".$parts['host'].$parts['path'];

Upvotes: 2

SotirisTsartsaris
SotirisTsartsaris

Reputation: 638

a noob way to do this with explode implode

<?php
$url = "http://www.domain.com/test/reports/index.php";
$newurl = explode("/", $url);
array_pop($newurl);
implode("/",$newurl);
?>

Upvotes: 0

Yogesh Pingle
Yogesh Pingle

Reputation: 3665

To be thorough, you'll want to start with parse_url().

$parts=parse_url("http://domain.com/user/100");

That will give you an array with a handful of keys. The one you are looking for is path.

Split the path on / and take the last one.

$path_parts=explode('/', $parts['path']);

Your ID is now in $path_parts[count($path_parts)-1].

Upvotes: 0

kwelsan
kwelsan

Reputation: 1219

<?php
$url = 'http://www.domain.com/test/reports/index.php';
$file_name = basename($url);
echo str_replace($file_name, '', $url);
?>

Upvotes: 0

Related Questions