4

if I validate html or register web in any serch engine, I get 302 error.

The reason is a header() function. If I take it away, everything is fine with 200 OK status.

So the main problem is that I need this redirection for web to be multilingual.

The logic is next. When user enters the web page for the first time index.php - require_once a file with a function:

function cookies() {
    if (!isset($_COOKIE["lang"])){
    setcookie('lang','ukr', time()+(60*60*24*31)); 
    header('Location: index.php');
}}
cookies();

so the user sees a page already filed with a deafault language.

If there would be no redirection from require_once file the data from mysql won't be downloaded and user won't see any text.

The question: should I leave this with HTTP 302 or rebuild the whole site/logic not to have any redirects at index page???

Coderbit
  • 701
  • 3
  • 9
  • 31
  • Well, you are redirecting _from_ index.php _to_ index.php, when you don’t find a cookie named `lang` … and that’s a _very dumb idea_ – search engine robots don’t support cookies, so you send them into an endless loop. And for users who have their browsers set to not accept cookies, the same thing happens. You should choose a different approach – the info, which language to display, should not be in a cookie, but should be part of the URL. That’s the only way that search engines will be able to index the different language versions at all. – CBroe Sep 26 '13 at 14:57
  • is cookies() only on index.php ? if so, why do you need the redirect? just remove it xD – aleation Sep 26 '13 at 15:12
  • @CBroe Yes, I understand it now, I wasn't thinking of "no cookies" robots/users. I also don't like if stmth. like ?lang=eng would be present all the time in a address bar, but it looks like thats the only way out for the situation. THQ for your input. – Coderbit Sep 26 '13 at 16:40

3 Answers3

5

302 is not an error. It is the status code for "Found" (aka "The document you asked for is over here"). PHP will insert this for you automatically if you add a Location header (unless insert a status manually, but you don't want a 301 here)

This is the expected response if you are telling people to go and get a different document based on their language preferences.

It is odd to redirect from index.php to index.php though. Presumably you should just return the appropriate document directly instead of redirecting.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Yes, I understand what you are talking about. The thing is, that php is server side, so when the index.php is opened for the first time it needs to check if (!isset($_COOKIE["lang"])). So if it's the first visit, definitely cookie's not set. So after setting it up, the index.php needs to be reloaded (because it's already going on not on a server side). I haven't figured out better solution rather then to take header() into other php.file, and reload index.php this way (already with cookies and language setted up). – Coderbit Sep 26 '13 at 14:41
  • You just treat "no cookie" as if it were the value you just set for it. Then carry on as normal. – Quentin Sep 26 '13 at 15:16
  • I guess it's the easiest way, to output the language type value, before the user sets the cookie. Thou it's still limited if the cookies are off. Thanks for your input. Probably I'd better use [GET] superglobal for language. – Coderbit Sep 26 '13 at 16:47
1

I got it. It's actually pretty simple.

The validators don't accept cookies. So they get stuck in a an infinite loop.

You can test this:

  1. delete all your cookies from your computer.
  2. Disable cookies in your browser and try loading your website.
Rayhan Muktader
  • 2,038
  • 2
  • 15
  • 32
  • There's a redirect setup. I pinged the website and got 91.206.200.202. But putting that in the browser gives me a 502 error. So, typing that url actually redirects elsewhere. The validators can't deal with that. I even received a 302 redirect error when I put in www.ethnocase.com/index.php. If you are not setting up these redirect rules then you should contact your hosting provider. – Rayhan Muktader Sep 25 '13 at 15:32
  • Thanks for your input! who's "in charge" of this redirects? Is that all about name servers? or I can set it up manually in my hosting options??? Can't get where this problem is coming from!? – Coderbit Sep 25 '13 at 17:14
  • You need to contact your hosting provider first. In your case that should be DELTA-X LTD. – Rayhan Muktader Sep 25 '13 at 17:43
  • ok! It's kinda comlicated. Hoster replied that it's a problem in code of web engine. Since web site is working fine in the browsers, I'm still looking for the answears about giving back an HTTP 302 response. – Coderbit Sep 26 '13 at 13:16
  • I found a trouble )) Edited a question. – Coderbit Sep 26 '13 at 14:18
  • @A1exandr : check my new answer. – Rayhan Muktader Sep 26 '13 at 15:06
0

Whenever You use header("location: .... you will get a 302, it's a status and not an error, it's telling the browser that the site has redirected the page:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Read those validators and engines and see if having the 302 is a problem for whatever you are trying to do, normally it shouldn't be.

A dirty way would be to force the header, personally I don't encourage this and don't know what side-effects could it have really, but it could be a quick workaround to trick those engines:

function cookies() {
    if (!isset($_COOKIE["lang"])){
    setcookie('lang','ukr', time()+(60*60*24*31)); 
    header('Location: index.php');
    header('HTTP/1.1 200 OK'); // <--- Forcing the header status
}}
cookies();
aleation
  • 4,796
  • 1
  • 21
  • 35
  • Yes, header('HTTP/1.1 200 OK'); may be the way, but you right, it's kinda "dirty way" )) I'll think of it as a last option )) – Coderbit Sep 26 '13 at 14:43