I want to change the language without showing the url:
http://myweb.com/?lan=AL
or
http://myweb.com/about-us?lan=AL
How can it be done in background, not to show in url.
This is the code below.
require('_inc_lang/lan_en.php');
require('_inc_lang/lan_al.php');
require('_inc_lang/lan_de.php');
if(!isset($_SESSION['lan'])){
session_start();
}
if(isset($_GET['lan'])){
$_SESSION['lan'] = $_GET['lan'];
}
$lan = isset($_SESSION['lan']) ? $_SESSION['lan'] : 'al';
switch ($lan) {
case 'al':
$TEXT = $TEXT_AL;
break;
case 'de':
$TEXT = $TEXT_DE;
break;
case 'en':
$TEXT = $TEXT_EN;
break;
}
You can do it based on the browser language
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "de":
$TEXT = $TEXT_DE;
break;
case "en":
$TEXT = $TEXT_EN;
break;
case "al":
$TEXT = $TEXT_AL;
break;
default:
$TEXT = $TEXT_EN;
}
?>
Change from GET to POST.
<form method="post">
..button/select/whatever have you
</form>
if(isset($_POST) && /* sanitise */)
$_SESSION['lan'] = $_POST['lan'];
Code needs a tidy up but you can do that yourself :) Is this what you are looking for?
Edit:
Absolutely totally must use a link or you will explode? The following SO pages will magically show you how!
Use a normal link to submit a form
How to submit a form with JavaScript by clicking a link?
You're already copying the language choice to the session, so if it is found in the URL, just location: to the version without the URL:
if(isset($_GET['lan'])){
$_SESSION['lan'] = $_GET['lan'];
header("Location: ".$_SERVER['SCRIPT_URI']);
}
Edit: note that I added variable $_SERVER['SCRIPT_URI'] instead of hard coded location; now it will work regardless of where it is called (SCRIPT_URI will give you server/page without query string)
Related
I'm coding a CDN in PHP, it works this far, but I want to choose different versions by adding an '#' with the version behind it. How do I get the version String after the '#'.
Example: https://cdn.steven2105.de/jquery#3.3.0
<?php
$error = false;
$path = str_replace("/", "", $_SERVER['REQUEST_URI']);
switch($path) {
case "jquery":
$toOpen = "jquery/3.3.1/jquery.min.js";
break;
case "jquery#3.3.0":
$toOpen = "jquery/3.3.0/jquery.min.js";
break;
case "vue":
$toOpen = "vue/2.6.10/vue.min.js";
break;
case "bootstrap-css":
$toOpen = "bootstrap/4.3.1/bootstrap.min.css";
break;
case "bootstrap-js":
$toOpen = "bootstrap/4.3.1/bootstrap.min.js";
break;
case "baguettebox-css":
$toOpen = "baguettebox/1.11.0/baguettebox.min.css";
break;
case "baguettebox-js":
$toOpen = "baguettebox/1.11.0/baguettebox.min.js";
break;
case "popper":
$toOpen = "popper/1.14.7/popper.min.js";
break;
default:
$error = true;
break;
}
if (!$error) {
header("Location: https://cdn.steven2105.de/libs/$toOpen");
} else {
include_once "website.php";
}
EDIT: What should I do now? It doesn't work.
I have different libaries in the path https://cdn.steven2105.de/libs/ e.g. https://cdn.steven2105.de/libs/jquery with files in it such like https://cdn.steven2105.de/libs/jquery/3.3.1/jquery.min.js. I'd like to access these links with a shorter prefix like https://cdn.steven2105.de/jquery#3.3.1. If I use the '#' character it should redirect to the version in the directory https://cdn.steven2105.de/libs/jquery/ and if it's without the '#' it should redirect to the latest version.
What is the easiest way?
You can use explode function for this. It Outputs as an array and fetch it.
<?php
$URL = ' https://cdn.steven2105.de/jquery#3.3.0';
$Result = explode('#', $URL);
echo("<pre>");print_r($Result[1]);
?>
Instead of using PHP you could probably fix this with webserver configuration.
Rewrite rules in Apache (https://httpd.apache.org/docs/2.4/rewrite/remapping.html) should do the trick. I would expect Nginx to offer similar functionality.
If you use Apache or Nginx to do this for you it will be way faster as you won't have the overhead of running a PHP script.
In my header file I have a switch statement set up to set the page titles. I want to keep it all in the header for easy maintenance, instead of adding a page title variable to each page.
My site is setup with the pages separated into folders, so www.example.com/ and www.example.com/about/ and so on. I'm using the following code: (I'm currently building the site in a subfolder called "alt" hence the first case being alt not empty.)
<?php
$base_url=basename(dirname($_SERVER['PHP_SELF']));
$base_page = basename($_SERVER['PHP_SELF'], ".php");
switch ($base_url) {
case "alt":
switch ($base_page) {
case "index": $title = "Home Page"; break;
case "400": $title = "400"; break;
case "403": $title = "403"; break;
case "404": $title = "404"; break;
default: $title = "Error";
}
case "about": $title = "About Page"; break;
default: $title = "Error";
}
?>
But now I'm adding in error pages, like a 404 page and I'm not sure how to set page titles for them since they are all in the main folder with the main index page.
Thanks.
The updated code is now correct. I needed to nest the switch statement and add a break after it.
I have a web site made in 3 languages, but will (maybe) be expanded.
So I created a table on the database where I insert the languages of the Website.
As index.php I have a script. If the URI detected is "/" the script redirects to /language/index.php.
I'd like now to have a script which dinamically redirects based on HTTP_ACCEPT_LANGUAGE.
My problem is that, the following script I made works only with the first foreach run.
$abbr = $link->query("SELECT abbr AS langs FROM LANGS");
while($langs_db = mysqli_fetch_array($abbr)){
$site_langs[] = $langs_db['langs'];
}
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
foreach($site_langs as $lang){
switch($client_lang){
case $client_lang == $lang:
header("location: http://www.mysite.com/$lang/index.php");
break;
default:
header("location: http://www.mysite.com/$lang/index.php");
break;
}
}
I thought a solution like:
case $client_lang == $lang:
$find "location: http://www.mysite.com/$lang/index.php";
break;
default:
$not_find = "location: http://www.mysite.com/$lang/index.php";
break;
And then:
if($find != ''){
header($find);
}else{
header(not_find);
}
But I'm not sure this it's a good idea...
Thank you!
// Set the default language as a fall back
$default_lang='en';
// Have an array with all languages your site supports
$langs=array('en', 'es', 'pt', /* etc... */);
// Query the browser's first preferred language
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// See if the language is supported otherwise redirect use the default one
$client_lang = (in_array($client_lang, $langs) ? $client_lang : $default_lang);
header("Location: /$client_lang/index.php");
Alternatively though you may want to consider gettext:
http://php.net/manual/en/book.gettext.php
I usually use this code below to include the page that I need into the body of my website to include the page when clicking on a link.
<?php
switch($_GET['page']){
case '1':
if(file_exists('main.php'))
{
include_once('main.php');
break;
}
default:
include_once('main.php');
break;
}
?>
but then I have to change this everytime i add a menu item by adding a case '2' ... etc
and now my question can this be written shorter/dynamically so that i just can add a link without having to change the piece of code everywhere?
ps: i did made it a little bit shorter.. but its still not good enough i think..
i also want to add this: i get my links from a ini file. i place it in there like this:
[navigation]
main.php = "Home"
if (!isset($_GET['page'])) {
$_GET['page'] = 'main.php';
}
switch ($_GET['page']){
case 'main.php':
case 'about.php':
case 'portfolio.php':
case 'tips.php':
$file = $_GET['page'];
break;
default:
$file = '404.html';
}
include_once $file;
is it possible to get this too from the ini file?
Try this:
$page = isset($_GET['page']) ? $_GET['page'] : "main.php";
if( file_exists($page)) include($page);
else include("404.html");
I’m working on a site and I kept getting a
Notice: Undefined index: id in file root on line 3
I know its something simple but can’t figure out where the problem lies exactly.
here is the code:
<?php
switch($_GET['id']) {
default:
include('pages/hello.php');
break;
case "testimonials":
include('pages/testimonials.php');
break;
case "faq":
include('pages/faq.php');
break;
case "raq":
include('pages/raq.php');
break;
case "contact":
include('pages/contact.php');
break;
}
?>
line 3 would be <?php switch($_GET['id']) {
any help would be greatly appreciated!
This is because in your url id=x is not always set, so when your trying to switc hthe value its not there. what you should do is like so:
<?php
$id = isset($_GET['id']) ? $_GET['id'] : ''; //This is just a short if-else
switch($id)
{
default:
include('pages/hello.php');
break;
case "testimonials":
include('pages/testimonials.php');
break;
case "faq":
include('pages/faq.php');
break;
case "raq":
include('pages/raq.php');
break;
case "contact":
include('pages/contact.php');
break;
}
?>
Basciall $id = isset($_GET['id']) ? $_GET['id'] : ''; what this says is, IF id is within the url then use that, otherwise use an empty string, the reason for the empty string is it will trigger the default: within the switch statement
this way $id will always be set to something.
Make sure that id is set and the default should go at the end:
if (!isset($_GET['id'])) {
include('pages/hello.php');
}
else{
switch($_GET['id']) {
case "testimonials": include('pages/testimonials.php'); break;
case "faq": include('pages/faq.php'); break;
case "raq": include('pages/raq.php'); break;
case "contact": include('pages/contact.php'); break;
default: include('pages/hello.php'); break;
}
}
For security reasons, make sure to sanitalize yor $_GET['id']. I would suggest you to setup an array of allowed pages and include those that are in the array. You can use in_array function for that.
It seems that the url parameter 'id' is not defined, that's the cause of the notice.
You should first check for its existence, eg.
if (isset($_GET['id'])) {
your code here
}