Another one of those header output problems.

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
I have a language selection which first sees if $_GET['lang'] is set, or a cookie is set.
PHP:
<?php
if (!defined('ROOT'))
    die;

 $text['english']          = "English";
 $text['trad-chinese']     = "中文";

// here you add the languages.

ob_start();
global $defaultLang;

/*
if(isset($_GET['lang'])) {
   require $_GET['lang'] . ".php";
   setcookie("saveLang", $_GET['lang'], time()+3600*24*30, '/'); // cookie that will expire in 1 month
} else if(isset($_COOKIE["saveLang"])) {
   require $_COOKIE["saveLang"] . ".php";
} else if(!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
   require $defaultLang .".php";
   setcookie("saveLang", $defaultLang, time()+3600*24*30, '/');
} else {
   $language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
   $language = substr($language,0,2); // splitting languages....
   if(( $language == "en") or ( $language == "tch")) { // checking if language is valid .. here you can change or add the languages
      require $language. ".php";
      setcookie("saveLang", $language, time()+3600*24*30, '/');
   } else {
      require $defaultLang . ".php";
      setcookie("saveLang", $defaulLang, time()+3600*24*30, '/');
   }
}
*/

// CHECKS AND MAKES THE LANGUAGE SELECTION A GLOBAL VARIABLE ALONG WITH A YUMMY COOKIE
if(isset($_GET['lang'])) {
   if(file_exist($_GET['lang'].'.php')) {
      $language = $_GET['lang'].'php';
   } else {
      $language = $defaulLang;
   }
} elseif(isset($_COOKIE["saveLang"])) {
   $language = $_COOKIE["saveLang"];
} else {
   $language = $defaultLang;
}
setcookie("saveLang", $language, time()+3600*24*30, '/');
define('LANG', $language);

//BRINGS IN THE LANGUAGE FILES
require ROOT.'sources/language/'.LANG.'.php';


ob_end_flush();
?>

the commented part, is code i've taken from a tutorial here on x10, can't get a link to it right now though. but it worked without the header error.

the error i get is:
Code:
[B]Warning[/B]:  Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\mcs\connect.php:11) in [B]C:\xampp\htdocs\mcs\sources\language\setlanguage.php[/B] on line [B]47[/B]

which happens to be this line:
PHP:
setcookie("saveLang", $language, time()+3600*24*30, '/');

here is part of the page calling it:
PHP:
require_once ROOT.'config.php';
require_once ROOT.'sources/language/setlanguage.php';
require_once ROOT.'sources/functions/home.php'; 
include ROOT.'sources/functions/gallery.php';?>

config.php
PHP:
<?php

if (!defined('ROOT'))
    die;

/* DB Connection */
$dbName = '';
$dbUsername = '';
$dbPassword = '';
$dbPort = '';
include ROOT.'connect.php';

/* IMPORTS */
// include ROOT.'sources/functions/functions.php';

$defaultLang = "en";
date_default_timezone_set('');

define ('HTTPDIR', '');

?>

connect.php
PHP:
<?
// connect to the database server
if (!($db = mysql_pconnect($dbPort, $dbUsername , $dbPassword))){
  die("Can't connect to database server.");    
}else{
  // select a database
    if (!(mysql_select_db($dbName, $db))){
      die("Can't connect to database.");
    }
}
?>



so after all that heap of code, can anyone make heads or tails of why it won't allow me to set a cookie. because i like cookies and its bad if I can't have one. xD
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
If any of the files you include before you try to send headers has a space after a closing

?> 'invisible' space here

that will cause the problem.
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
thank you descalzo,
had a space in the connect.php line 11. these little things catch you off guard

but now a new problem has arisen (sp?):
I set $lang if it checks and passes to a DEFINE

PHP:
define('LANG', $language);

//BRINGS IN THE LANGUAGE FILES
require ROOT.'sources/language/'.LANG.'.php';

but for some reason, LANG is just being pulled in as nothing, so I end up getting a error not found error.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Can you set a constant with a variable in PHP?
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
o I didn't even think about it like that.
I wish there is a way you can,
for some reason I just like the way the constant looks, it can be global and you don't need a $ sign
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
PHP:
if(isset($_GET['lang'])) {
   if(file_exists($_GET['lang'].'.php')) {
      $language = $_GET['lang'];
   } else {
      $language = $defaulLang;
   }
} elseif(isset($_COOKIE['saveLang'])) {
   $language = $_COOKIE['saveLang'];
} else {
   $language = $defaultLang;
}

can anybody seem to get this, because it looks alright to be
but for some reason $language is not registering.
and $defaultLang is set in config.php
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
PHP:
if(isset($_GET['lang'])) {
   if(file_exists($_GET['lang'].'.php')) {
      $language = $_GET['lang'];
   } else {
      $language = $defaulLang;  <<<<<<<<<<<<<<<<<<<<<<<<
   }
} elseif(isset($_COOKIE['saveLang'])) {
   $language = $_COOKIE['saveLang'];
} else {
   $language = $defaultLang;
}

can anybody seem to get this, because it looks alright to be
but for some reason $language is not registering.
and $defaultLang is set in config.php

Dropped a t
Edit:

Ok. Was asking as much as stating. Most languages don't allow you to it.
 
Last edited:

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
sweet!

thank you guys very much!

rewards will be given soon. give it a day, im tired rite now after staring at that code for hours.
 
Last edited:
Top