Jump to content

optimisation


Matpatnik

Recommended Posts

Hi guys,is there a better of doing this code?

$string = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];// set default languageif (!isset($_GET['lang']) && !isset($_COOKIE['lang'])) {	$lang = "fr";	setcookie('lang', $lang);	header("Location: http://www.lespetitsoursons.ca/index.php?section=index〈=$lang");} else if (isset($_COOKIE['lang'])) { // get the cookie	$lang = $_COOKIE['lang'];	if ($lang != $_GET['lang'] && (!empty($_GET['lang']) || !isset($_GET['lang']))) { // if language have been changed		$lang = $_GET['lang'];		setcookie('lang', $lang);	} else if (!isset($_GET['lang']) && strlen($string) >= 54) { // if no $_GET['lang'] in url but in a section page		$string .= "〈=$lang";	} else if (!isset($_GET['lang'])) { // if no $_GET['lang'] in url and no section page		$string .= "index.php?section=index〈=$lang";	}} else { // no cookie detected	$lang = $_GET['lang'];}// select languageif ($lang == "en") {	@include ("language/english.php");	$self_fr = "<a href=\"http://" . str_replace("lang=en", "lang=fr", $string) . "\">$Francais</a>"; // text link	$self_en = "$English"; // text} else if ($lang == "fr") {	@include ("language/francais.php");	$self_fr = "$Francais"; // text	$self_en = "<a href=\"http://" . str_replace("lang=fr", "lang=en", $string) . "\">$English</a>"; // text link}

The code work perfectly. I wander if I should make a function from this? if yes what will it look like (I'm not used to build function)Do you guys have any ideas, suggestions?

Link to comment
Share on other sites

Well, for starters, I'd avoid redirects. They really slow the stuff. And besides, redirecting to the same file only to specify default variables is useless. You could just set the variables if they are not set and be done with it.The rest is just too messy for me to figure out what you're doing, but if you're using PHP5 and can install extensions, or are on a host that would do that for you, try out the HTTP extension. The thing that's particularly relevant to your script is it's ability to negotiate language but that's just the tip of the iceberg.

Link to comment
Share on other sites

This line is a little weird:if ($lang != $_GET['lang'] && (!empty($_GET['lang']) || !isset($_GET['lang']))) {I'm not sure about that logic. You're checking if the cookie is different from the querystring, and if the querystring is not empty or not set. I'm not sure about that last part. I don't think you would want to update anything if the querystring is not set.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...