I want to get the only server name with directory in php.. can someone help me ..?

Heading Here

for example :-
i hava a page which url is
www.demo.com/123/page.php
I want only :- www.demo.com/123/

can some one tell me how can i get the only server name without page name ..

Thanx

Member Avatar for diafol

parse_url

just join the host and the path items. Alternatively, use pathinfo:

$path = pathinfo($file, PATHINFO_DIRNAME);

Is what you're trying to do is to prettify the URL?

Like you have 6 pages in your website, all writtenin PHP:

  1. www.mydomain.com/page1.php
  2. www.mydomain.com/page2.php
  3. www.mydomain.com/page3.php
  4. www.mydomain.com/page4.php
  5. www.mydomain.com/page5.php
  6. www.mydomain.com/page6.php

and you, for example, wanted it to look this way, not exactly the same format as this though:

  1. www.mydomain.com/page/1/
  2. www.mydomain.com/page/2/
  3. www.mydomain.com/page/3/
  4. www.mydomain.com/page/4/
  5. www.mydomain.com/page/5/
  6. www.mydomain.com/page/6/

is that what you want?
or you simply wanted to make your index.php, home.php or page.php (Directory indexes) resulted into something where the filename's not shown? like this one.

web_root/home/index.php
BROWSER ADDRESS: www.mydomain.com/home/

web_root/product/index.php
BROWSER ADDRESS: www.mydomain.com/product/

web_root/contact/index.php
BROWSER ADDRESS: www.mydomain.com/contact/

Anyway, both involves editing your HTACCESS file.

Dear gon1387

u did nt understand my probleum .. i want serername with direcorty name..
for example :-
in my browser url link is

www.demo.com/web/abc.php

**in this example u can see
www.deme.com (Server or domain name)
www.demo.com/web (/web is directory name)
www.demo.com/web/abc.php (abc.php is page name)
**
now what i want ..
i want to display or get only the server or directory name

www.demo.com/web

not with the page name ..

Thanx

Member Avatar for diafol

So, did you try pathinfo?

Hi chira9na9pal,

I believe I did do misunderstood you, you were refereing to just parsing string of the servername and with the resource path from the URL; while, I was thinking of editing htaccess and stuffs. Whoosh.. that was way to far. LOL

Anyway, as diafol suggest, use parse_url and pathinfo. It's your best bet. Unless you would like to find the last occurence of forward slash with the filename and file extention using strrpos then subtract it from there; but forward slashes and '.' will be a problem if it's part of your query string. If your working with files and folders and passing it through your URL.

Both returns array. For parse_url, you'll be working with its scheme, host and path. Then, for pathinfo, just the dirname.

here's the doc for the two:
Parse URL
Path Info

It just occured to me after I posted my reply. Sorry for polluting the thread.

You can use $_SERVER's SCRIPT_NAME as your reference in strrpos than just providing the scriptname yourself. It's more accurate as it won't jumble with the query string, just in case you're passing directory name and filename as value there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.