genzedu777 Posted April 29, 2010 Share Posted April 29, 2010 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 Quote Link to comment Share on other sites More sharing options...
cs.punk Posted April 29, 2010 Share Posted April 29, 2010 '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.. xD Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted April 29, 2010 Author Share Posted April 29, 2010 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? Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted April 29, 2010 Share Posted April 29, 2010 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 Quote Link to comment Share on other sites More sharing options...
cs.punk Posted April 29, 2010 Share Posted April 29, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted May 3, 2010 Author Share Posted May 3, 2010 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 Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted May 3, 2010 Share Posted May 3, 2010 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 Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 3, 2010 Share Posted May 3, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tazerenix Posted May 3, 2010 Share Posted May 3, 2010 hmm, i always thought REQUEST_URI returned the full url! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.