PHP add GET variable to links - php

I have a page with links that looks like this:
...
I also may have a GET variable ?lang=en in the current url. How can I add the GET variable ?lang=en to all the links from the page, without adding them manually or adding a variable to each link? Thanks.

set $_ENV['lang'] = 'en' in your basic ( assume config.php ) file
and retrieve by
getenv('lang') or $_ENV['lang']
Refernece

Add lang=en to session variable as $_SESSION['lang']='en' & initialized it in every page.

You could use output buffering or an Apache filter to parse your output to automatically add the lang query string parameter to all links, however, this isn't terribly efficient.
If you really do not want to add it manually to each link then I suggest you store it in a session variable.
At the start of each page (perhaps in a generic include script) you could have something like:
<?php
define('DEFAULT_LANG', 'en_GB');
session_start();
// check if a new lang has been specified.
if (isset($_GET['lang'])) {
// yes, so use the requested lang
$_SESSION['lang'] = $_GET['lang'];
// otherwise, check if a lang was previously set
} else if ( ! isset($_SESSION['lang']) ) {
// no, so use default lang:
$_SESSION['lang'] = DEFAULT_LANG;
}
?>
You should then use $_SESSION['lang'] instead of $_GET['lang'] in the rest of your page. Also, you may want to add some sort of validation to ensure the requested lang is valid and available.

Related

check if a querystring exists, if not create one - PHP

I have several pages which use querystrings to highlight an option in a menu, all the url's on the page have the currant querystring phrased in them so the same menu option will be highlighted on the next page if the user clicks the link.
However the problem arrises when someone visits the page without the querystring included in the url, the menu option isn't highlighted.
What i would like to do is check the URL to see if a querystring is present, if one isnt, create one.
The url's are phrased as such www.mysite.co.uk/Folder1/Folder2/Page.php?id=3
and i would like the default querystring to be ?id=1 if one isn't already present in the url.
Any ideas on how you do this?
And what would happen if a user visits using the URL www.mysite.co.uk/Folder1/Folder2/Page.php?
Would the URL end up as www.mysite.co.uk/Folder1/Folder2/Page.php??id=1
or would it be www.mysite.co.uk/Folder1/Folder2/Page.php?id=1
Thanks,
Maybe there are plenty of ways. You can assign value to $_GET key if one does not exist. Or if you really need to query string, you can renavigate the user to the same page with present querystring.
if (!isset($_GET['id'])) {
header("Location: Page.php?id=1");
exit;
}
It should be before any output in the page. So if user visits Page.php or Page.php? or Page.php?someDifferentParamThanId=10 it will return false on isset($_GET['id']) thus it will redirect to Page.php?id=1
This should work:
if(isset($_GET['id'])){
//It exists
}else{
//It does not, so redirect
header("Location: Page.php?id=1");
}
Do something like:
if(!isset($_GET['id'])){
header('LOCATION:www.mysite.co.uk/Folder1/Folder2/Page.php?id=1'); die();
}
In php, the query string is loaded into $_REQUEST variable. In your case, $_REQUEST['id'] will be equal to 1, 3 or whatever you get in the query string.
For solving the problem when no id is given via GET, I think will be enough to add this line at the beginning of each php page:
<?php
if ( $_REQUEST['id']=='' ) {$_REQUEST['id']=1;}
?>
It is not necessary to change the URL on the fly.

PHP: A way to manually switch languages?

I have a website that has the following language switching algorithm:
First, it detects the default browser language (I do not know why? but Chrome always gives something like en-EN,ru,lv, so Chrome's default language always is English, it seems).
Then it writes the language value into a session variable lang and requests the desired string file (i.e. /assets/includes/en-US/strings.php);
And every string from this file is being included in the HTML code, so the pure HTML has not any plain text in.
Of course, a default language detection is not the reason to stop - I need a manual language switcher like links (LV | EN | RU). So, what id the possible (and maybe the best) way to switch the language and to overwrite the session variable after user clicks to the desired language?
The best way is the simpliest way :)
$langs = array('LV', 'EN', 'RU');
<?php foreach ($langs as $lang): ?>
<?=$lang;?>
<?php endforeach; ?>
so you give the user opportunity to change lang via GET in this example.
Overwrite the session to the sent request:
<?php
if(in_array($_GET['lang'], $langs) {
$_SESSION['lang'] = $_GET['lang']; // to prevent user to change its session to something you don't want to
}
?>
Afterwards you just interact with this session to display content.
You can use redirection, if you have each page written in different language:
(but I guess the logic how to interact with the language you have already implemented from the automatic language detection, but still... let me suggest some ways at fast?)
<?php
if (isset($_SESSION['lang']) && $_SESSION['lang'] !== 'EN') {
header("Location: mysite.com/".$_SESSION['lang']."/index.php");
exit;
}
?>
Or, you can use translation method.
All of your translations are in a database under columns with the same names as your $langs array.
So you output the content from this particular column:
SELECT lang_{$_SESSION['lang']} FROM translations WHERE string = '$string';

Appending $_GET variable to URL already containing $_GET variables

I have a sub-navigation menu on my page that opens up whenever ?n=review is appended to the URL.
I also have languages changing and session variables being set whenever ?language=xx is appended.
However, if I am on a page with the sub-nav open, and I wish to change language from there, the ?n=review is replaced with ?language=xx upon link click, and therfore the sub-nav closes. How would I check to see if a $_GET variable is already set every time the language is changed, and if so, append my additional one to the end?
Once the language has been changed, the new language is set to a session variable so I do not need to worry about keeping it's $_GET variable. I'm wondering if I would need to store the sub-nav variable in the same way...?
Sorry if this question is worded badly, I'm trying to get my head around the logic myself.
If you need to see any code or tidier wording, please let me know before down-voting.
if(empty($_GET['language'])) {
$fragment = '?n=review';
} else {
// if language is set, get the value
$lang = $_GET['language'];
$fragment = "?language=$lang&n=review";
}
// I just made up the /index.php part, replace that with the proper link.
$url = "/index.php$fragment";
Get variables can be chained by using the '&' Character. So you could build your link that way:
echo "./?n=review&language=xx"
If you want to know, if any $_GET parameters have been set, you can simply use PHP's empty() function like this:
if( empty( $_GET ) )
{
//$_GET is empty
}
else
{
//$_GET has something in it
}

Help on changing the page language without changing the current page (PHP)

langauage web. I set 3 links, Français... (href=changeLanguage.php?lang=fr,es,en)
changLanguage.php
session_start();
if(isset($_SESSION['bckTo']) && isset($_GET['lang'])){
$lang = preg_replace('/[^a-z]/','',trim($_GET['lang']));
#TODO
#More vlidation ...
$full_url = $_SESSION['bckTo'];
$full_url = str_replace(array('&lang=en','&lang=es','&lang=fr'),'',$full_url);
header('Location: '.$full_url.'&lang='.$lang.'');
}
$_SESSION['bckTo'] save the current URL for example: mysite.com/index.php?id=x&n_d=y
The problem is, the header translate the URL to: mysite.com/index.php?id=x&n_d=y&lang=en.
Any idea will be appreciate
Why not just set the language in session instead of via GET? Then you just have to put a link to change the language and and then redirect to the page. This would probably be best, given that you are already using sessions.
Example:
English
On the changeLanguage:
//code up here
if (isset($_SESSION['bckTo') && isset($_GET['lang'])) {
// $lang code here
$_SESSION['lang'] = $lang;
header('Location: ' . $_SESSION['bckTo']);
}
Then you would just need to change your language checking / displaying code to check the session variable rather than the GET (on the actual pages).
Running html_entity_decode will convert those HTML entities back into ampersands.
http://us2.php.net/manual/en/function.html-entity-decode.php

I don't know what to call it php/css error tho

whats going on is everything is loading just fine url is deigned.sytes.net except for the links when i click about us or services or contact they look like there loading but the content in body.tpl doesn't change from default. maybe you can help me with this why the links are not changing. you u want here are the ONLY php files
I have made phps files for view perpose's but if you insist on it i will post the require code.
designed.sytes.net/index.phps
designed.sytes.net/classes/file.class.phps
In the URLs you name the parameter p but in your files.class.php you actually test for $_GET['page']. So either change the URLs to use page as parameter or change the code to:
// in files.class.php instead of if(!isset($_GET['page']))
if(!isset($_GET['p'])){
// your code here...
} else {
// ...
}
In your original code, as $_GET['page'] does never exist, it always shows the index page.
Another thing that looks strange to me is the following (but maybe it is just how you set it up):
if(file_exists($_GET['page'].'.txt')){
// and lets include that then:
ob_start();
include("contents/". $_GET['page'] . '.php');
$content = ob_get_contents();
ob_end_clean();
}
You first check whether the text file e.g. about.txt exists but then include a PHP file contents/about.php. Is this intended? Does the PHP always exist if the text file exists?
UPDATE:
Also make sure that you properly check the value that you get from $_GET['page'] or however you call it in the end.
E.g. this call http://designed.sytes.net/index.php?page=../index seems to kill your server (sorry it was unintentionally :( )
UPDATE 2:
In order to provide "some" security you could check whether $_GET['page'] is one of predefined values instead of checking whether a file with this name exists. E.g:
$valid_pages = array('home', 'about', 'services', 'contact');
if(isset($_GET['page']) && in_array($_GET['page'], $valid_pages) {
// include page here
}
else {
// redirect to home page
}
That makes sure that $_GET['page'] is not of form of relative pathes like ../index. If it is not one of those values in $valid_pages you redirect to the home page.
I see in your http://designed.sytes.net/classes/file.class.phps file you have $_GET['page'] but on the query string you have p=
an example of what does not work is:
http://designed.sytes.net/index.php?p=about
and then changed to:
http://designed.sytes.net/index.php?page=about
seems to show something different.

Categories