Simple Machines Community Forum

General Community => Scripting Help => Aiheen aloitti: kL - joulukuu 08, 2006, 09:15:26 IP

Otsikko: whats the problem with this code?
Kirjoitti: kL - joulukuu 08, 2006, 09:15:26 IP

$fullurl = $_SERVER['FULL_URL'];

if (!strpos($fullurl, 'http://www.'))
{header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com/"); }



it just keeps reloading the page on a loop. :(
Otsikko: Re: whats the problem with this code?
Kirjoitti: Jacen - joulukuu 09, 2006, 07:10:53 AP
I assume because your second header line is OUTSIDE the if block
Otsikko: Re: whats the problem with this code?
Kirjoitti: SleePy - joulukuu 09, 2006, 10:33:43 AP
@jacen.. Its all fine still, He has brackets around it doesn't mater.

@k_talk
http://php.net/strpos

From what php.net says I don't think you should use that..

Try:


if (eregi('http://www.', 'http://'.$_SERVER["HTTP_HOST"]))
      {
      header("HTTP/1.1 301 Moved Permanently");
      header("Location: http://www.mysite.com/");
      }

http://php.net/eregi
:D
Otsikko: Re: whats the problem with this code?
Kirjoitti: Jacen - joulukuu 09, 2006, 02:57:48 IP
Isn't the tabs important for PHP? (the space, whatever)
Otsikko: Re: whats the problem with this code?
Kirjoitti: SleePy - joulukuu 09, 2006, 09:18:43 IP
no..  in fact php ignores whitespace unless you have it in "".

The spacing and all is just for normal human readability (im not normal so I can read it like php does)
Otsikko: Re: whats the problem with this code?
Kirjoitti: Jacen - joulukuu 09, 2006, 11:23:07 IP
Fair enough.
Otsikko: Re: whats the problem with this code?
Kirjoitti: Rudolf - joulukuu 10, 2006, 02:11:38 IP
Lainaus käyttäjältä: k_talk - joulukuu 08, 2006, 09:15:26 IP

$fullurl = $_SERVER['FULL_URL'];

if (!strpos($fullurl, 'http://www.'))
{header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com/"); }



it just keeps reloading the page on a loop. :(

Using eregi for this is a huge overkill. It's really much slower.
Use:

$fullurl = $_SERVER['FULL_URL'];

if (strpos($fullurl, 'http://www.') === false)
{header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.mysite.com/"); }


The reason is that the strpos returns the position of the string which is 0 if the search string starts with the searched one. And (!0) is still true. So it keeps reloading the page.
Strpos returns "false" if the string is not found, === will check if the values are equal and of the same type - boolean in this case.