How to trigger language switcher - php

Hi i would like to set several languages on my site, a have good looking php script but i have a problem with trigger it, my knowledge of php is very poor
language.php
header ("content-type: text/html; charset=utf-8");
if (isset($_GET['lang'])) {
$langID = $_GET['lang'];
setcookie('lang', $langID, time()+(3600*24*365));
}
elseif (isset($_COOKIE['lang'])) {
$langID = $_COOKIE['lang'];
}
else {
$langID = 'Pl';
}
and my index.php
<body>
<div id="languages">
<img src="images/en.png" />
<img src="images/pl.png" />
</div>
<div id="content">
<div id="tresc">
<?php if($langID == 'Pl'): ?>
<div class="editable" id="polska">
<h1>Head</h1>
<p>first land</p>
</div>
<?php else: ?>
<div class="editable" id="angielska">
<h1>Head</h1>
<p>second lang</p>
</div>
<?php endif; ?>
</div>
</div>
</body>
what I should put here to trigger script
<div id="languages">
<img src="images/en.png" />
<img src="images/pl.png" />
</div>

<img src="images/en.png" />
<img src="images/pl.png" />

And, for the sake of all that is holy, please check that the vale of $_GET['lang'] is acceptable before putting it in the cookie:
if (in_array($_GET['lang'], array('pl','en','fr')) {
$langID = $_GET['lang'];
} else {
// throw an error
}
If you don't do this your script will not be secure.

Related

Spot the typo in this code snippet causing page to not load, php, html - probably a missing or misplaced character?

I've been editing the following block of code and now when I use it then the page will not load but I also don't get anything in the error log either. I am guessing this has happened due to a missing or misplaced character somewhere but have looked and looked over it and cannot spot what it is! does it stand out to anyone?
<?php if ($logourldecoded != "") { ?>
<?php if ($companyname != "") { ?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } else { ?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php } else { ?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } ?>
I also know it's not that any of the data I'm calling with $NAME_HERE being incorrect or missing as if i just echo them all indiviually they are exactly as expected and before my edits where showing.
Missing } to close the inner IF
<?php
if ($logourldecoded != "") {
?>
<?php
if ($companyname != "") {
?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
} else {
?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php
// the missing line
}
?>
<?php
} else {
?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
}
?>
You might consider not placing <?php ..?> tags on each line, it makes the code almost completely unreadable, and whats unreadable is by definition unmaintainable

Passing variables between PHP scripts

I have problem and can not understand why this not working. The language not transferred and I see empty area.
In my PHP:
<?php
$_SESSION['Language'] = $_GET['Spanish'];
header("Location:../index.php");
exit;
?>
In index.php:
<?php
$Language = $_SESSION['Language'];
include 'header.php';
echo '<div id="content">
<img src="Images/SplashScreen.jpg" id="bgImage" alt="City" width="1280" height="720" />
<div class="splashOuterRingsOverlay" id="contain">
<img src="Images/Loading-outer-circle.png" id="image1" alt="inner ring" width="282" height="282">
</div>
<div class="splashInnerRingsOverlay" id="contain">
<img src="Images/Loading-inner-circle.png" id="image2" alt="outer ring" width="282" height="282">
</div>
<div class="splashButtonOverlay">
<!--<button type="button" id="splashButton" onclick="loadData(20)"></button>-->
<button type="button" id="splashButton" onclick="loadLangCustom(\'Spanish\')"></button>
</div>
<div class="splashTitleOverlay">
<p id="splashTitle">'.$Language.'</p>
<!--<p id="splashTitle">Please press BERMAD icon</p>-->
</div>
</div>';
include 'footer.php';
?>
If you set the session variable you have to add the session_start at the top of the php page.
session_start()
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
Change your code like this
<?php
session_start();
if (isset( $_GET['Spanish'])) {
$_SESSION['Language'] = $_GET['Spanish'];
header("Location:../index.php");
exit;
}
?>
Add session_start() in the beginning of your code
--
<?php
session_start();
$_SESSION['Language'] = $_GET['Spanish'];
<?php
session_start();
$Language = $_SESSION['Language'];
You are missing session_start() in index.php and my.php

Can't get login feature to work

I'm currently learning PHP and am creating a small CMS feature that includes a login area. I have used the code below which includes an include header file that contains the doctype/head info and the opening tag. It also includes the header content. I also have a connection file for connecting to the db.
My header include code is:
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title><?php echo $pagetitle ?></title>
<link rel="stylesheet" href="../stylesheets/foundation.css">
<link rel="stylesheet" href="../stylesheets/app.css">
<style>#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800); #import url (http://fonts.googleapis.com/css?family=Kreon:100,200,300,400);</style>
<script src="../javascripts/modernizr.foundation.js"></script>
</head>
<body>
<div class="subHeader">
<div class="row">
<div class="four columns logo">
<img src="../images/logo.png" alt="logo" />
</div>
<div class="eight columns navigation right">
<ul class="navigationMain">
<li class="<?php if($navcurrent == "home"){echo "navigationActive";} ?>">Home</li>
<li class="<?php if($navcurrent == "services"){echo "navigationActive";} ?>">Services</li>
<li class="<?php if($navcurrent == "work"){echo "navigationActive";} ?>">Recent Work</li>
<li class="<?php if($navcurrent == "about"){echo "navigationActive";} ?>">About</li>
<li class="<?php if($navcurrent == "contact"){echo "navigationActive";} ?>">Contact</li>
</ul>
</div>
<div class="twelve columns titlesection">
<h2><?php echo $headTitle ?></h2>
<h4><?php echo $headsubTitle ?></h4>
</div>
</div><!--End Feature Row-->
</div><!--End Feature-->
<div class="underbar">
<div class="bordertriangle"></div>
<div class="row">
<div class="eight columns"> </div>
<div class="three columns right socialcontainer">
<ul class="socialicons">
<li><a><img id="linkedinIcon" src="../images/socialli.png" alt="linkedin icon" /></a></li>
<li><a><img id="twitterIcon" src="../images/socialtw.png" alt="twitter icon" /></a></li>
<li><a><img id="facebookIcon" src="../images/socialfb.png" alt="facebook icon" /></a></li>
</ul>
</div>
</div>
When I open the admin page, the username password form, header and footer appear as they should. If I test the errors, they return as they should. However, when I successfully log in using a valid username and password, no content appears except the what is included in the header file. Can anyone point me in the direction of what i might be doing wrong? Any help would be much appreciated. I am a relative noob to PHP...
<?php
$pagetitle = "Admin";
$navcurrent = "home";
$headTitle = "ADMIN AREA";
$headsubTitle = "SITE ADMINISTRATION AREA";
include_once('../includes/connection.php');
include_once('../includes/headeradmin.php');
if (isset($_SESSION['logged_in'])) {
echo('Successfully Logged In');
} else {
if (isset($_POST['username'], $_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username) or empty($password)){
$error = 'An Error Has Occurred - All Fields Are Required';
}
else{
$query = $pdo->prepare('SELECT * FROM users WHERE user_name = ? AND user_password = ?');
$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->execute();
$num = $query->rowCount();
if ($num == 1) {
$_SESSION['logged_in'] = true;
header('location: index.php');
exit();
}
else{
$error = 'The username/password you entered was incorrect - Please try again';
}
}
}
?>
<div class="row">
<div class="four columns centered">
<?php if (isset($error)) { ?>
<h5 style="color: #e63333;"><?php echo $error; ?></h5>
<br />
<br />
<?php } ?>
<form action="index.php" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Login" />
</form>
</div>
</div>
You can't use the header('location: index.php'); line if you've already output content (i.e - html code) to the browser when you included the header in this line include_once('../includes/headeradmin.php');
read the documentation of header - Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
you need to redirect the user with the header() function before you output the head html of the admin page

Require once php function and variables

I am looking for some help using the PHP function. At the moment my website is structured like this:
index.php:
<?php
require_once( 'page_elements.php' );
?>
<body>
<?php echo content();?>
</body>
page_elements.php:
<?php
function content() {
?>
<div class='main'>
<img class='main' src="<?=$ImgName?>"> </img>
</div>
<?php
} ?>
if statement:
if (isset($_SESSION['basket_total']))
{
$basket_total = $_SESSION['basket_total'];
if($basket_total !== '0')
{
$ImgName = 'img/basket.php';
}
else
{
$ImgName = 'img/basket_empty.php';
}
}
What I want to do is to be able to define $ImgName in an if statement that isn't involved in the function content() but if i include it in another file, include 'if_statement.php' or in another function then it doesn't recognise the variable.
Sorry, its my first time structuring a website like this and I am finding it a bit confusing.
Cheers in advance
First of all, you don't close an an "img" tag with another "img" tag ...
function content(){
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
is the proper way of doing things. Now as to your question, I'm having trouble understanding your goal, but do you perhaps mean something a.la ...
function content(){
$imgname = include "file.php";
echo'
<div class="main">
<img class="main" src="'.$imgname.'" alt="" title="">
</div>
';
}
and the if_statement.php would be something like ...
if(isset($_SESSION['basket_total'])){
return $_SESSION['basket_total'];
}else{
return "img/basket.php";
}
This will get around the current issue you are having, but I would do like Ionut Flavius Pogacian suggested above and look into an MVC
<?php
require_once( 'page_elements.php' );
$image_name = "batman.jpg";
?>
<body>
<?php echo content($image_name);?>
</body>
page_elements.php:
<?php
function content($image_name) {
?>
<div class='main'>
<img class='main' src="<?=$image_name?>" />
</div>
<?php
} ?>

How to let the user choose a page number?

I've added information to some boxes, which are alligned with the 'float' attribute.
The content of all those boxes together is too big to fit in the webpage, so I want to put those boxes on different pages, and to let the user choose which page he wants to view.
E.g: Choose page: 1 2 3... (last page)
The contents of these 'pages' must be in divisions.
For reference, see my css code:
.clear {
clear: both;
}
#over-ons .imagebox,
#over-ons .contentbox
{
background-color: #C4C4A6;
float: left;
margin: 0 10px 40px 0;
padding: 10px;
border: 2px solid black;
}
#over-ons .imagebox {
width: 100px;
text-align: center;
}
#over-ons .contentbox {
width: 200px;
}
Also, see my HTML code of the 'boxes' I mentioned:
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Function: blablablabla<br />
Name: blablabla<br />
City: Breda<br />
Education: HBO Informatica<br />
</div>
<div class="clear"></div>
How do I create the ability to let the user view 3 different boxes, depending on the page number he wants to see?
Edit:
My current solution looks like this, only nothing happens when I click 'page 2':
<?php
//Vraag pagina op
$page = 1;
if (isset($_POST['page']) && is_numeric($_POST['page']))
$page = $_POST['page'];
?>
<? if ($page == 1) { ?>
<div class="imagebox"><img src="media/images/overons/pieter.png" /></div>
<div class="contentbox">
Functie: Bedrijfsleider<br />
Naam: Pieter<br />
Stad: Deurne<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/patrick.png" /></div>
<div class="contentbox">
Functie: Verkoper<br />
Naam: Patrick<br />
Stad: Roosendaal<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/rick.png" /></div>
<div class="contentbox">
Functie: Ontwikkelaar<br />
Naam: Rick Gommers<br />
Stad: Oosterhout<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else if ($page == 2) { ?>
<div class="imagebox"><img src="media/images/overons/bas.png" /></div>
<div class="contentbox">
Functie: Salesmanager<br />
Naam: Bas<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/robbert.png" /></div>
<div class="contentbox">
Functie: Administrateur<br />
Naam: Robbert Tuerlings<br />
Stad: Goirle<br />
Opleiding: HBO Bedrijfskundige Informatica<br />
</div>
<div class="clear"></div>
<div class="imagebox"><img src="media/images/overons/timothee.png" /></div>
<div class="contentbox">
Functie: Implementatie Technicus<br />
Naam: Timothee<br />
Stad: Breda<br />
Opleiding: HBO Informatica<br />
</div>
<div class="clear"></div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
Imagine your current page is called index.php. So first of all you want to create the links to your pages in your HTML as the following:
Page 1
Page 2
Page 3
And for a clear code you can simply put the content of your "boxes" in an array and then output the corresponding entry for the page:
<?php
$content = array{ "Page 1 data", "Page 2 data", "Page 3 data" };
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']))
$page = $_GET['page'];
?>
<div class="contentbox">
<? echo $content[($page - 1)]; ?>
</div>
Another solution (less easy to read and to maintain, but easier if your content is really complex) you can simply fetch the page number and in a regular if else display the box they requested.
<?php
//Fetch the page
$page = 1;
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0)
$page = $_GET['page'];
?>
<? if ($page == 1) { ?>
<div class="contentbox">
Your first box for page 1
</div>
<? }
else if ($page == 2) { ?>
<div class="contentbox">
Your second box for page 2
</div>
<? }
else if ($page == 3) { ?>
<div class="contentbox">
Your third box for page 3
</div>
<? }
else { ?>
<div class="contentbox">
You didn't select a proper page!
</div>
<? } ?>
But ye, there are 1001 ways to do this. It really depends on your needs and the way you want to do it. The more you hardcode, the uglier the code usually gets.. ;-)
One way is to have all divisions on the page and make only the one visible which user has clicked to...To do so you can make all 3 divs and by default make them hidden and by javascript trigget action...
Another way is by PHP's output buffering to accomplish this:
ob_start(); // begin collecting output
include 'myfile.php';
$result = ob_get_clean(); // retrieve output from myfile.php, stop buffering
$result will then contain the text..
The first method is preety easy one most people use it..But as you specificaly said you have created all separate pages for all 3 div's I had to add the php method..

Categories