Jump to content

Recommended Posts

Hi,

 

I would like to ask how to do this:

 

currently im working in my localhost

 

if I access my localhost the directory in it are:

clientname1

clientname2

 

now, my problem is

 

im in localhost/clientname2

how to get the clientname2 only?

 

 

    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

    $URL= $protocol.'://'.$_SERVER['HTTP_HOST']."/";

 

this will display http://localhost/

but I want to include clientname2

 

  $URL= $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

 

it will display

 

  http://localhost/clientname2/pages/product.php?page=about

 

how to display this only http://localhost/clientname2/

 

Link to comment
https://forums.phpfreaks.com/topic/106142-solved-help-php-directory-handling/
Share on other sites

break the directory apart with something like explode, and look for the numbered occurance your interrested in.  If its the directory thats one away from root then:

 

<?php
$directory = dirname($_SERVER["REQUEST_URI"]);
$dir_array = explode("/",$directory);
$lookingfor = $dir_array[1];
?>

function getBaseDirURL()
{
    $url  = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
    $url .= $_SERVER['HTTP_HOST'];

    $uri_path = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME);

    // get the base directory
    $pos = strpos($uri_path, '/', 1);

    $url .= ($pos !== false) ? substr($uri_path, 0, ($pos+1)) : $uri_path;

    return $url;
}

echo  getBaseDirURL();

thanks for all your help guys..

 

@phorman

 

yeah it works but my problem is

 

when im in this location

 

http://localhost/clientname2/

 

it displays nothing.

 

but when im in this location http://localhost/clientname2/pages/

 

it display clientname2

 

even im in this location http://localhost/clientname2/ or in this location http://localhost/clientname2/pages/

 

it should display clientname2

 

thanks guys..

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.