1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Small Problem - Phpechos showing up after 2nd Reload...

Discussion in 'PHP' started by misohoni, Dec 26, 2007.

  1. #1
    I don't know much about PHP, so could be small problem - but the changed code helped my site to be spidered better, but now the Phpecho files only display after reloading a 2nd time...strange!

    This was the working code before:

    <?php
                              if (!isset($_COOKIE['lang'])) {
    		if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "ja") {
    			header("Location: /setlang.php?lang=ja");
    		} else {
    			header("Location: /setlang.php?lang=en");
    		}
    	}
    	
    	//language stuff
    	
    	if ($_COOKIE['lang'] == "en") {
    		
    		//english array
    		
    		$l = array(
    		1 => "Logged in as", ...etc
    Code (markup):
    This is the changed code:

    <?php
    	function change_lang($la) {
    		if(strlen($la) == 2) {
    			setcookie('lang', $la);
    		}
    	}
    
    	if (!isset($_COOKIE['lang'])) {
    		if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "ja") {
    			change_lang("ja");
    		} else {
    			change_lang("en");
    		}
    	}
    	
    	//language stuff
    	
    	if ($_COOKIE['lang'] == "en") {
    		
    		//english array
    		
    		$l = array(
    		1 => "Logged in as",
    Code (markup):
    Can anyone spot the problem? Thanks
     
    misohoni, Dec 26, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    With setcookie('lang', $la); you are sending the cookie header and setting the language, but that cookie will only be read on subsequent page request. That's why the previous code was doing header("Location....") (the cookie was probably set there and there was a redirect back to calling page, to read that cookie)

    Maybe you could try something like this:
    
    function change_lang($la) {
    		if(strlen($la) == 2) {
    			setcookie('lang', $la);
    		}
    	}
    
    	
    if (!isset($_COOKIE['lang'])) {
    	if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "ja") {
    		change_lang("ja");
    		$lang = "ja";
    
    	} else {
    		change_lang("en");
    		$lang = "en";
    	}
    }
    else $lang = $_COOKIE['lang'];
    
    //language stuff
    
    if ($lang == "en") {
    
    PHP:
    Replace all further $_COOKIE['lang'] with $lang
     
    hogan_h, Dec 26, 2007 IP
    misohoni likes this.
  3. misohoni

    misohoni Notable Member

    Messages:
    1,717
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    200
    #3
    Thanks man, that seemed to work!
     
    misohoni, Dec 26, 2007 IP
    hogan_h likes this.