I have faced a problem. The code writes in the html body parts. I want the site to come up as by default when the site is opened. At the moment I click on these codes and it shows up when I click on the page and click on the page again. I want the page to always open in English by default.
<?php
session_start();
if($_GET['lang']) {
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
}
if ($_SESSION['lang'] == "en") {
$lang= "en";
}
elseif ($_SESSION['lang'] == "gr") {
$lang= "gr";
}
else {
$lang= substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
}
include 'languages/'.$lang.'.php';
?>
"I want the page to always open in English by default"
Does this do the trick?
if (! isset($_SESSION['lang'])) {
$_SESSION['lang'] = 'en';
}
By default $lang= substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2); will work. If you want by default English then change this to $lang= "en".
Related
I want to communicate with the session on two pages that I dynamically create on a Wordpress theme. I do this on a common header.php file, but I can't get the session value I sent from one page on the other. Could you help?
$pageName = $_SERVER['REQUEST_URI'];
if($pageName == "/test/"){
$_SESSION["1cKullaniciLogin"] = "TEST DENEME";
header("Location: /faaliyet-raporlarimiz/");
exit();
}
if($pageName == "/faaliyet-raporlarimiz/"){
if(ISSET($_SESSION["1cKullaniciLogin"]))
{
}
else
{
header("Location: /test/");
}
}
I am coming to you to find out if it is possible to change the path in a URL using a PHP variable.
My goal would be to organize my website with 2 folders (one for each language), choose the language on a random page in one of the two folders and be able to change the language by modifying the URL.
For example, I'm on http://truc.com/fr/test.php; I change the language and I get http://truc.com/en/test.php.
I've done a test that works but I'd prefer something more dynamic, that will work on all the pages of the website:
fichier index.php
<?php
session_start();
if (!isset($_SESSION['lang']))
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "fr";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang']))
{
if ($_GET['lang'] == "fr")
$_SESSION['lang'] = "fr";
else if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
}
if($_SESSION['lang']==fr)
{
header('Location: http://localhost/site/languages/fr/index.php');
}
else if($_SESSION['lang']==en)
{
header('Location: http://localhost/site/languages/en/index.php');
}
?>
Adjusted your code and added a new variable $page_language that handles the page language and redirects to specific link for various languages and before redirecting to the page a session language is set to remember it on next page . Good luck
if (isset($_SESSION['lang'])) {
$page_language = $_SESSION['lang'];
} elseif (isset($_GET['lang']) && !empty($_GET['lang'])) {
$page_language = $_GET['lang'] ;
} else {
$page_language = "fr" ; //set default language if no session and url has language
}
if ($page_language == "fr") {
$_SESSION['lang'] = "fr"; //set session language for later visits
header('Location: http://localhost/site/languages/fr/index.php');
exit;
} elseif ($page_language == "en") {
$_SESSION['lang'] = "en"; //set session language for later visits
header('Location: http://localhost/site/languages/fr/index.php');
exit;
}
My website has two languages (English and German) between which a user can chose by clicking a button. It then sets a cookie to store this decision for 24h. I'm also using the php-i18n class from GitHub. It works by storing all language texts in specific *.ini files. According to the chosen language, either the German or English text gets displayed via PHP variables. I hope I explained it well enough.
That all works, except for my "legal details" page. Since this page has a lot of text, I made two versions of this one page, one English, one German. Depending on what language is chosen, I want to display the according page version. But it somehow always ends up being the "default German" version. I'm probably missing something very obvious as to why my code isn't working.
PHP in the header (dynamic, shows up on all pages before all else):
require_once 'i18n.class.php';
$i18n = new i18n('lang/lang_{LANGUAGE}.ini', 'langcache/', 'de');
// Parameters: language file path, cache dir, default language (all optional)
$i18n->setForcedLang('de');
// language buttons & set cookie
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
$i18n->setForcedLang('de');
setcookie('language', 'lang-de', time() + 86400, '/');
echo "german cookie set!";
}
if ($lang == 'en') {
$i18n->setForcedLang('en');
setcookie('language', 'lang-en', time() + 86400, '/');
echo "english cookie set!";
}
}
// get language cookie
if (isset($_COOKIE['language']) && $_COOKIE['language']== "lang-de") {
echo "german cookie is really set, now get it!";
$i18n->setForcedLang('de');
} else if (isset($_COOKIE['language']) && $_COOKIE['language']== "lang-en") {
echo "english cookie is really set, now get it!";
$i18n->setForcedLang('en');
} else {
echo "no cookie set";
}
// init object: load language files, parse them if not cached, and so on.
$i18n->init();
PHP on the legal details page:
include 'templates/header.php';
// show english or german legal details
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
echo "k, german impressum";
include 'impressum-de.php';
} else if ($lang == 'en') {
echo "english impressum";
include 'impressum-en.php';
}
} else if ($language == 'de') {
echo "german impressum b/c cookie";
include 'impressum-de.php';
} else if ($language == 'en') {
echo "english impressum b/c cookie";
include 'impressum-en.php';
} else {
echo "default German impressum";
include 'impressum-de.php';
}
The buttons:
<div class="language">
DE/
EN
</div>
Does anyone have some input? Ideas how to improve the code are also always much appreciated since I'm still learning.
Try This: (not "else if" -> its "elseif")
include 'templates/header.php';
// show english or german legal details
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
echo "k, german impressum";
include 'impressum-de.php';
} elseif ($lang == 'en') {
echo "english impressum";
include 'impressum-en.php';
}
} elseif ($language == 'de') {
echo "german impressum b/c cookie";
include 'impressum-de.php';
} elseif ($language == 'en') {
echo "english impressum b/c cookie";
include 'impressum-en.php';
} else {
echo "default German impressum";
include 'impressum-de.php';
}
Help please organize bi-lingual website.
So first there are two files eng.php, es.php and they will be stored in translation site.
example:
$lang['hi'] = 'Hi';
How can I organize further language choice on the site and record information about the language in cookies?
You can have two files like this.
Source of en.php:
$lang = array(
'hi' => 'Hi'
);
Source of es.php:
$lang = array(
'hi' => 'Hello'
);
And for the main content file, the source should be this way:
<?php
session_start(); // Make sure you initialize cookies / session
$allowedLangs = array('en', 'es'); // Array with allowed values
if(isset($_SESSION['lang'])) { // If already user had stored language in session
include $_SESSION['lang'] . ".php";
} elseif(isset($_GET['lang']) && in_array($_GET['lang'], allowedLangs)) { // If user had requested like index.php?lang=en
include $_GET['lang'] . ".php";
$_SESSION['lang'] = $_GET['lang']; // Update the session with the language
} else { // If user is visiting for the first time, then...
include "en.php";
}
echo $lang['hi'];
?>
<?php
if(!isset($_COOKIE['lang'])){
?>
Choose Language...
ESENG
<?php
} else {
if($_COOKIE['lang']=='es'){
header("location:es.php");
}
elseif($_COOKIE['lang']=='eng'){
header("location:eng.php");
}
}
?>
es.php // eng.php
<!--Your Content-->
<?php
setcookie("lang","es/eng",time()+SECONDS_YOU_WANT)
?>
i'm trying to put a piece of code into my login script. If the users account is a particular type i want it to redirect to another url.
This bit i know how to do. but i want the url window to open in a pretty photo box which i have on my site. It's a kind of css box iframe window. I have the majority of my links opening in them by using this code:
Link
But when i try and do that for my redirect function it won't work. Can anyone let me know why this would be, i am typing the code like this:
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
<?php
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type))
if ($acctype['account_type'] == 'free_member') {
redirect_to("chatboard.php?iframe=true&height=260\" rel=\"prettyPhoto[1]\"");
}
?>
Ok so a couple things and I am not sure how you have it setup, but this is how it should be
<?php
function redirect_to( $location = NULL ) {
if ($location != NULL) {
header("Location: {$location}");
exit;
}
}
$account_type = account_type();
while ($acctype = mysql_fetch_array($account_type)){
if ($acctype['account_type'] == 'free_member') {
redirect_to('chatboard.php?iframe=true&height=260&rel=' . $prettyPhoto[1]);
}
}
?>
And on your chatboard.php check for the proper variables making sure they are cleaned from possible attacks as well.