Did I miss anything?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Did I miss anything?

Post by neophyte »

Code: Select all

<?php
if ($_SERVER['HTTP_HOST'])//don't redirect if this variable doesn't exsist empty etc.
{
	if ($_SERVER['HTTP_HOST'] !=  $_SERVER['SERVER_NAME'])
	{
		header("Location: http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
	}
}

?>


Did I miss anything? I'm trying to redirect my page from http://www.somesite.com to just http://somesite.com. Just wondering if this is right or if I could do it a better way....

Thanks
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Code: Select all

<?php
ob_start();
if ($_SERVER['HTTP_HOST'])//don't redirect if this variable doesn't exsist empty etc.
{
    if ($_SERVER['HTTP_HOST'] !=  $_SERVER['SERVER_NAME'])
    {
        header("Location: http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); exit();
    }
}

?>
Try that.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Cool I've never seen ob_start before. I don't get it though. So ob_start prevents anything but headers to be output before the redirection occurs? This script is sitting before the <html> tag so does it do anything additional to have ob_start on the script?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

ob_start() stops all output... so that nothing is sent out untill certian functions are called like exit() etc... read the documentation on php.net for more info

(I always use ob_start, that way I dont have to worry about "headers already sent")

Also on errors I do this:

Code: Select all

<?php
ob_clean();
echo ("Error');
exit();
?>
That way I don't get a 1/2 rendered page
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

prob 90% of the problems posted on this board that contain the use of header(),
are due to people sending headers after they have sent output.

ob_start() can fix thier problem. he prob just near rightfully assumed that was likely your problem. (i would have guessed the same)

it looks to me like your code should work. it might work too good though, if for example you using subdomains.

this would not fail when using subdomains, as it would only match the subdomain "www"



Code: Select all

<?php

if (isSet($_SERVER['HTTP_HOST'])) {
    if (0 === strpos(strtolower($_SERVER['HTTP_HOST']), 'www.')) {
        header("Location: http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    }
}

?>
but still, i think your code should work.

i would try doing this to see what the _SERVER variables actually contain. different configs can sometimes return unexpected results. HTTP_HOST may not even exist on your server....

Code: Select all

print_r($_SERVER);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

My code is working. I posted mostly to get feed back about ways I can bullet proof what I wrote. As you probably know, that's the real trick isn't it? But I think I've learned a lot from your posts. I didn't even know about the ob function set. I think the idea for using an exit statement after the header location is a good idea. And I really wanted to know about 'HTTP_HOST' whether that was dependable as a variable to test against. So I really appreciate your tip Rehfeld. Thanks all for all your responses.
Post Reply