I'm trying to rework my company's old website. Currently it supports two languages - English and Hebrew. The way it works is that it simply runs include_once on a language file "lang_en.php" or "lang_he.php" depending on what is contained in $_SESSION['lang'], which would be either 'en' or 'he'.
Right now I'm working on the first pages - Index.php (homepage), Login.php and Register.php. All 3 of them start out exactly alike with a bit of a nested include call.
<!DOCTYPE html>
<html lang="en">
<?php
include '../Common/HtmlHead.php';
?>
The file HtmlHead.php is where I run session_start(), include the file LanguageParser and do some other buildup for the html part of the pages.
<?php
session_start();
include_once("../../Controller/Config.php");
include_once("../../Tests/ObjectModels/ServerResponse.php");
include_once("../../Tests/ObjectModels/Users/User.php");
include_once("../Language/LanguageParser.php");
?>
Then finally is the file LanguageParser.php which looks into GET and SESSION and determines which language file to run include_once on:
if (isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}
// Include Language file
if (isset($_SESSION['lang'])) {
$langFile ="lang_" . $_SESSION['lang'] . ".php";
if (file_exists($_SERVER['DOCUMENT_ROOT']."/Logic/SmartView/Language/".$langFile)) {
include_once $langFile;
} else {
include_once "lang_en.php";
}
} else {
include_once "lang_en.php";
}
Yet for some reason, the language saved in $_SESSION['lang'] just doesn't seem to stick. Each page behaves as if it has its own session with its own language saved. Even if I change language, navigate to another page, then navigate back, the language reverts.
The session cannot start when some output has been sent to the client. You can make a trick and use $_SESSION before any HTML or echo call.
LanguageParser.php
<?php
session_start();
$lang = 'en';
if (isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}
if (isset($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
}
?>
<!DOCTYPE html>
<html lang="<?php echo $lang?>">
<?php
include '../Common/HtmlHead.php';
?>
HtmlHead.php
// Include Language file
$langFile ="lang_" .$lang . ".php";
if (file_exists($_SERVER['DOCUMENT_ROOT']."/Logic/SmartView/Language/".$langFile)) {
include_once $langFile;
} else {
include_once "lang_en.php";
}
Related
login.php
<?php
ob_start();
session_start();
include '../config.php';
if( (isset($_SESSION['can-id']) )) {
header('location: ../home/profile.php');
}
if(isset($_POST['can-login']))
{
$email=$_POST['email'];
$password=$_POST['password'];
$sql="SELECT * FROM `user_credentials` WHERE `email`=:email AND `password`=:password";
$pdoResult=$conn->prepare($sql);
$pdoExec=$pdoResult->execute(array(":email"=>$email,":password"=>$password));
$pdoResult->setFetchMode(PDO::FETCH_ASSOC);
$count=0;
$uid='';
while ($r=$pdoResult->fetch()) {
# code...
$count+=1;
$uid=$r['email'];
}
if ($count==1) {
# code...
$_SESSION['can-id']=$uid;
header('location: ../home/profile.php');
}
else
{
$_SESSION['error']="login failed";
}
}
?>
<html>
....
</html>
profile.php
<?php
ob_start();
session_start();
if (!(isset($_SESSION['can-id']))) {
# code...
header('location: ../login/');
}
else
{
$cid=$_SESSION['can-id'];
}
?>
<h1 ><?php echo $cid;?></h1>
This is my code after log in the page was redirected to profile.php page but in profile page session variable doesn't printed I don't know why but this problem was not occuring every time I log in It occurs sometimes so I can't find what is the problem. Anyone knows please help me to solve the problem.
Remove ob_start() from your login.php
Don't put session_start() in all of your file
e.g login.php, profile.php, etc
but instead, add this to your config.php for example:
<?php
session_start();
//.. config variables here
Then, include config.php also in your profile.php.
in the root of my project folder i have a file called index.php and a directory called views which contains a file called page_one.php ...
index.php file has the follwing code:
<?php
$action = "one";
if ($action == 'one') {
$name = "John";
//include './views/page_one.php';
header("Location: views/page_one.php");
}
?>
and page_one.php has the following code:
<?php echo 'Name = ' . $name; ?>
in the above code I have commented out the line with include because that works perfectly..I want to pass the value of $name using the header function..Is there a way of doing it WITHOUT sending the value in the URL?
I want the address in the URL to change when page_one.php is accessed that is why I am using the header function instead of include...
#Qirel as per said, try using session. Do something like this in index.php
<?php
session_start();
$_SESSION['name'] = "John";
header("Location: views/page_one.php);
exit();
?>
and inside page_one.php
<?php
session_start();
echo $_SESSION['name'];
unset($_SESSION['name']); // remove it now we have used it
?>
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 am trying run a script called random_post_generator.php which should execute every time a user is logged in.
I am using this approach as an alternative to cron.
Here is how my session is currently created:
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
}
?>
But how do I say - "if session is active, then run this script"?
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
include 'random_post_generator.php';
}
?>
or you can use require 'random_post_generator.php'
If I understood correctly, you are trying to find out how to include a script of php (that is located in an outside .php file) inside your current file while using your previous code that checks if a user is logged in:
<?php
$root_directory_path = $_SERVER['DOCUMENT_ROOT'];
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
$pathName = $root_directory_path."myScript.php";//I am assuming here
//the script is located inside the root directory, and not in a sub
//directory
require($pathName);
}
?>
just remember that whatever php code is inside myScript.php has to have the <?php ?> tag surrounding it. Your code does not reuse the <?php ?> tag of the "calling" file.
Let me know if that worked for you
I'm trying to write a php script that checks the language(which is defined by the language function in $language) for a value and if user requests any address, www.example.com/foo/bar/data.php?=foobar it will redirect him by http refresh or redirect or header location(not preferable) to subdomain.example.com/$1 ($1 as in the same original requested address).
something like this but without the header location:
<?php if ($language == "en") { header ("Location: http://"$language".example.com/"$1""); } ?>
this does not work, also I get an error in the log "header already sent by another file" which is another script I got running and cannot change the code.
So, what I need is a script that reads the variable and according to its value it will redirect the user to the appropriate subdomain.
Hi you can echo a javascript code that contains redirection. Try this one.
<?php
if($language === "en"){
echo "<script type='text/javascript'> document.location = 'http://' . $language . '.example.com/' . $1; </script>";
}
?>
This code works most for me compared to header('Location: etc...').
<?php
// Language detection code
// ...
if ('en' === $language)
{
header('Location: http://' . $language . '.example.com' . $_SERVER['REQUEST_URI']);
exit();
}
If you really can't change the code from the other file, you'll need to make an html redirection instead.
<?php
// Language detection code
// ...
if ('en' === $language)
{
$url = 'http://' . $language . '.example.com' . $_SERVER['REQUEST_URI'];
echo <<<EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Refresh" content="0; url={$url}" />
</head>
<body></body>
</html>
EOF;
exit();
}