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

 

 

Link to comment
Share on other sites

'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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.