Jump to content

$_SERVER['HTTP_HOST'] & ($_SERVER[' PHP_SELF' ] ), need help in these codes


genzedu777

Recommended Posts

Hi everyone,

 

I am in the midst of redirecting my page once user log in/log out. However I don't really understand this coding. Could any explain it to me?

 

$home_url = ' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php' ;

header(' Location: ' . $home_url) ;

 

Generally, it should be redirected to http://dress-a-holic.com/index.php

 

May I know what will the individual functions produce?

1. $_SERVER['HTTP_HOST']

2. dirname($_SERVER[' PHP_SELF' ])

 

Does $_SERVER['HTTP_HOST'] produces, dress-a-holic.com?

 

What does dirname($_SERVER[' PHP_SELF' ]) produces?

 

Thanks,

Wilson

 

 

'PHP_SELF'

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

 

 

'HTTP_HOST'

Contents of the Host: header from the current request, if there is one.

 

So yes, HTTP host would return something like holic.com..

 

:rtfm: xD

http://www.dress-a-holic.com/index.php

 

$home_url = ' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

 

' http: //' . $_SERVER['HTTP_HOST'] will give http://dress-a-holic.com/

Am I right?

 

dirname($_SERVER[' PHP_SELF' ] ) will give index.php

Am I right?

 

$home_url = ' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

So why do we still need to declare /index.php again?

 

take a url example.com/foo/bar/foobar.php

 

dirname($_SERVER['PHP_SELF']) would return /foo/bar/

basename($_SERVER['PHP_SELF']) would return foobar.php

$_SERVER['PHP_SELF'] would return /foo/bar/foobar.php

so dirname removes the file in the url whereas basename() returns only the filename

 

 

http://www.dress-a-holic.com/index.php

 

$home_url = ' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

 

' http: //' . $_SERVER['HTTP_HOST'] will give http://dress-a-holic.com/

Am I right?

 

dirname($_SERVER[' PHP_SELF' ] ) will give index.php

Am I right?

 

$home_url = ' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

So why do we still need to declare /index.php again?

 

We? Is this from a tutorial ?

Hi Tazerenix,

 

Just take for example, www.example.com/foobar.php

 

I have tried meddling with the codes, and I realised if I removed

' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

 

I will get foobar.php

 

If I were to remove

' http: //' . $_SERVER['HTTP_HOST'] . dirname($_SERVER[' PHP_SELF' ] ) . '/index.php'

 

I will get www.example.com/index.php. It shows the correct URL, but...

 

I also realised by removing dirname($_SERVER[' PHP_SELF' ] ), I could not logout, and it is not working in my logout, it seems like even though the url reflects www.example.com/index.php, there is something wrong.

 

Do you understand what i'm talking about?

 

 

Thanks

its probably not a good idea to remove dirname($_SERVER['PHP_SELF']) because if a script is executing in a subdirectory it will remove that from the url. Although, im not entirely sure what your saying about the logout, can you give a bit more of an explanation

Usually, if you want to redirect users to the index page. You'd do..

header("LOCATION: /index.php");

 

Although..

$_SERVER['PHP_SELF']

Think of a form action, if it has PHP_SELF, and it works.. it's basically reloading that page.

 

Think of it like this..

$domain = $_SERVER['HTTP_HOST'];

$path = $_SERVER['REQUEST_URI'];

$domain = www.mydomain.com

$path = /page.php?something=something

It's basically like PHP_SELF, but it adds on the extras to the end of the pagename. (I believe PHP_SELF doesn't do it)

 

It's hard to say what you're trying to achieve, as we don't have a URL. We have the index page, i assume it should get redirected there.. if so, then the header method at the top will work fine, or this method will.

 

$domain = $_SERVER['HTTP_HOST'] . '/index.php';
header("LOCATION: $domain");

 

Although I don't understand what your problem is wth the logout.

What exactly happens? Any errors? If it just redirects without the user logging out. Then do a session_uset and a session_destroy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.