I have this PHP script. It's the only one that really worked to me:
<?php
/*Check_if_user_has_changed_language: */
if(isset($lang)){/*If_so:*/
setcookie("ling",$lang,time()-60*60*24*365,"/",".sayip.info",0);/*Wipe_previous_cookie*/
setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);/*Whatever_the_means_lang_has_been_stored,_store_latest_lang_in_new_cookie:*/
//echo "<script language=\"JavaScript\">alert('Selected language=$lang')</script>";/*UnComment_to_check*/
}else{/*If_user_has_NOT_changed_language:*/
if(isset($_COOKIE['ling'])){/*Check_if_user-language_cookie_is_set._If_so:*/
$lang=$_COOKIE['ling'];
setcookie("ling",$lang,time()-60*60*24*365,"/",".sayip.info",0);/*Wipe_previous_cookie*/
setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);
//echo "<script language=\"JavaScript\">alert('Cookie language=$lang')</script>";/*UnComment_to_check*/
}else{/*If_user-language_neither_selected_nor_in_cookie,_choose_browser_language:*/
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
setcookie("ling",$lang,time()+60*60*24*365,"/",".sayip.info",0);
//echo "<script language=\"JavaScript\">alert('Your browser language=$lang')</script>";/*UnComment_to_check*/
}
}
?>
First the code detects the language of the user's browser. That's ok.
Then stores the info in a cookie. That's ok.
Well in this piece of code its everything ok. What I really need is to create an option for visitors change the language. I was thinking something like linked flag images so when someone click on the flag it changes the language.
Can someone explain to me through an example or even a clean, full solution? My skills in PHP are poor.
Thanks in advance.
I would put the selected language in the URL, e.g http://example.com/en/foo/bar. This makes the selected language transparent and easy to change.
i would put to if i knew how to lol...Since i got this script working after testing more or less 10 different scripts, im not like falling back, just need an example in how to put flags and when someone click on the flag it changes de value of the cookie...
I'm not sure if I got your question right
if your gonna place a link for each language in your page, make the link something like
http://www.example.com/?lang=jp
then in the php code before the script that you posted add
if (isset($_GET['lang'])) $lang = $_GET['lang'];
is this what you ment?
A more elegant solution may be to check the user's headers. Most browsers will allow users to set their preferred language in preferences. This in turn sends an HTTP header with the request. The header looks like this.
Accept-Language : en-us,en;q=0.8,ar-ly;q=0.5,id;q=0.3
The value is a comma-delimited list of accepted languages, ordered by preference ( the q=x part is the preference). This way, you can automatically detect what language the user has opted to see the web, and display it if you have it.
Related
G'day,
This is for my tutorial purpose.
I have 3 files
1. mlogin.htm - Takes the input from the user (login name and password). The action is set to the next file so the details can be checked.
<form id="logIn" name="logIn" method="get" action="mlogin.php">
2. mlogin.php - Takes the value from mlogin.htm using GET method. If the details match the details in XML file, the user is redirected to the next file
$musername = $_GET['username'];
$mpassword = $_GET['password'];
exit(header('Refresh:5; url=mloginsuccess.htm'));
3. mloginsuccess.htm - Displays the menu.
Now, what I'm trying to do is to show the username in the 3rd file so it's something like
Welcome, John
I do realise that I can do this using a session by changing the 3rd file to a
mloginsuccess.php
but it MUST be a
mloginsuccess.htm
I was wondering if this is possible.
Any help is appreciated :)
Suppose for a moment that you actually do want to follow your instructions to the letter. (You don't really want to do this, probably... interpreting requirements, rather than following them exactly, is a key trait of a decent software engineer.) If your requirement is that you must use a static page, you have a couple options for getting data accessible on that page. All of which require JavaScript.
Cookies
Query String
Anchor Fragment
Basically, you need to set this data in one of these three places so that you can access it with JavaScript from your static HTML page later on. To set a cookie with PHP, use setcookie(). To read it with JavaScript, use document.cookie, or one of the many snippets of code to make this easier.
To set the query string, simply do so in your redirect:
header('Location: http://www.example.com/mloginsuccess.htm?name=' . urlencode($_GET['username']));
See this question for the JavaScript needed to read the query string: How to get the value from the GET parameters?
Finally, for the anchor fragment, you can often redirect to it the same way. (However note that not all browsers are guaranteed to follow the anchor fragment part of the URL!) To read the anchor fragment, use window.location.hash.
I hope that in the end, you will choose to do none of these and keep your auth logic in a sensible place. Literal interpretation of requirements rarely leads to good code and application design. At a minimum, you can hack around the URL requirement with a rewrite rule, making whatever.html be an alias to whatever.php. The client doesn't know or care what is actually running on the server... that's the server's job. I would tell you how to write a rewrite rule, but you didn't specify which server you are using, so I'll leave that part up to you to Google.
How can you expect to use a php feature(SESSION) in a file which is not php(.HTML).
However you are allowed to use html inside a php file as php is a template engine and process the html ...refer this for for indepth
What renders the HTML?
just convert your .html to .php and
<?php>
session_start();
$_SESSION['username']=$_GET['username']
?>
<html>....<body>welcome <?=$_SESSION['username']?></body>...</html>
or however your html tags are.
Maybe you can use AJAX to load session details. For example, using JQuery,
<script>
...
$(document).ready(function(){
$.ajax({
url: "load_session.php",
success: function(uname){
$("#uname").html(uname);
}
});
});
...
</script>
...
Welcome, <span id="uname"></span>
I'm owner of some web site with dozen of web pages. Pages were made by using PHP. Before some time I discovered that some guys by using Joomla CMS and wrapper menu option included starting (login page) there and on this way confused members and other visitors, especially because "window" of wrapper isn't enough big and some information on my page aren't visible. On this way visitors connect these pages with me and get bad feeling about whole my site. I contacted these guys but no answer, then I tried to solve it by using $_SERVER['HTTP_REFERER'] super variable but I didn't get right and working solution for this problem. Someone experienced similar problem? Thanks.
EDIT - This is the code
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
if ($HTTP_REFERRER) {
// check if the referrer is on your noentry list
// if so redirect it to another page
if ($HTTP_REFERRER == "www.mean.visitor.com") {
echo 'referer is' . $HTTP_REFERRER;
die;
} // shows the referrer and formats ur local harddrive echo "You came from $HTTP_REFERRER";
} else {
//everything is OK
}
from the code you posted the first problem i see it's on the first line:
$HTTP_REFERRER=%SERVER['HTTP_REFERER'];
should be
$HTTP_REFERRER=$_SERVER['HTTP_REFERER'];
Then in the second if you must insert the web addresses you want to block. so change
if ($HTTP_REFERRER == "www.mean.visitor.com")
with
if ($HTTP_REFERRER == "the address yo want to block")
And write die() instead of die.
has something changed?
I want to create a link like the following:
http://www.myurl.com/?IDHERE
What i want to be able to do is be able to goto the above link, and then pull whats after the ? (in this case IDHERE) and be able to use that information to perform a MySQL lookup and return a page.
Can anyone point me into the right direction? please know this is using PHP not ASP
The issue here is not with your scripting language, but with your web server setup. I'll refer to these by their Apache names, but the features should be available in most web servers.
There are three features you might want to use:
1) content negotiation (mod_negotiation), which allows your web server to try a specified list of extensions in a specified order, for example: http://example.com/foo might be http://www.example.com/foo.html or http://example.com/foo.php
2) DirectoryIndex, which tells the web server that when a client asks for http://example.com it should look for a specified list of files in order, so it might server up http://example.com/index.html or http:/example.com/index.php
3) mod_rewrite, which allows you to basically rewrite the URL format received by the server. This allows you to do things like translate http://example.com/foo/bar/baz to http://example.com/foo/bar.php?page=baz
The rest is done by the backend script code as normal.
Create a default PHP file in that directory that will get loaded when no file name is specified (e.g. index.php). In your PHP script you can get the part after the question mark from the variable $_SERVER['QUERY_STRING'].
Do the following in your site's main index.php:
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
In the link, form or whatever - index.php?id=someid
In your index.php file:
$_GET['id'] = $id
Now you can use it:
e.g.
echo $id;
Since it's your default page, it will work without the extension.
list($id) = array_keys($_GET);
// right now, $id represents the ID you're looking for.
// Do whatever you want with it.
this was exactly what i was looking for, though now i just need to create something to notify if nothing is there or not. Thank you all for your responses.
I would solve it by using .htaccess file if possible.
create a .htaccess file in the main directory with the content:
RewriteEngine on
RewriteRule cat/(.*)/(.*)/(.*)/$ /$1/$2.php?$3
that should translate "example.com/foo/bar/baz" to "example.com/foo/bar.php?page=baz"
I want to do this:
if (user from other website, like google, or input our website URL directly in the browser)
{
//redirect according to browser languge
if (!preg_match('/en-US/', $_SERVER['HTTP_USER_AGENT']))
{
wp_redirect("http://cn.gearor.com");
}
}
I don't know how to write the first if statement, I don't know how to get the from URL and how to check it's my website or other websites. If it's my website, do nothing, if it's not my website, check if the browser is English, if not, redirect to http://cn.gearor.com
You want something like:
if (!preg_match('%your_domain.tld%i', $_SERVER['HTTP_REFERER']))
{
if (!preg_match('/en-us/i', $_SERVER['HTTP_ACCEPT_LANGUAGE']))
// .. do your stuff here.
}
I changed your language check to use HTTP_ACCEPT_LANGUAGE, and to also not be case sensitive (Opera uses en-US where Firefox and IE use en-us in my tests).
You could also consider using /^en-us/i if you are checking for the DEFAULT language. The language strings can contain multiple languages, comma delimited.
$_SERVER['HTTP_REFERER'] is the full URL that led to the page that checks it. It's not guaranteed to be set, though it's the only way to know what you're looking for.
Also, you may want to check $_SERVER['HTTP_ACCEPT_LANGUAGE'] instead of $_SERVER['HTTP_USER_AGENT'] for the language of the user.
Here is the full reference to the $_SERVER superglobal array. You should check the keys, especially those starting with HTTP_.
My old web site has an index.html page … nothing strange! Everything is fine.
The new web site has an english and a french version, so the new index is index.php?lang=eng…. That makes sense.
I don’t like to make a front page that will say “english” or “french”. But that’s not good for ranking or seo.
So the question is: How do I manage to get a default index.php with request (?lang=eng) to become the front page?
domain.com/en/index.php
domain.com/fr/index.php
Use url rewriting with regular expressions (mod_rewrite, ISAPI, whatever) to handle requests to relevant pages so
domain.com/en/index.php REWRITE TO domain.com/index.php?lang=en
domain.com/fr/index.php REWRITE TO domain.com/index.php?lang=fr
This way your pages are two seperate pages to search engines but handled via one gateway in code. I'm not a regex expert but it would be a very simple regex I would imagine
I'm not sure I understand the question. It seems to have two parts:
How to provide a default language of English:
$lang = empty($_GET['lang']) ? "eng" : $_GET['lang'];
Do you also have a problem of where to put the English/Francais links so search engines don't ding you? I wasn't aware of this problem.
It might also help to let us know if you're using a CMS, and if so which one.
Unless I'm misunderstanding the question, in index.php, when you check the language, put something like this:
$lang = #$_GET['lang'];
if ( empty($lang) ) $lang = 'eng';
Just put an argument in the php code that says :
if (lang == "") // haven't done php in a while so the syntax is probably wrong
{
lang = "eng";
}
In other words, if there isn't an argument on the lang variable, you can just set it to be eng automatically, and so the first page will default to English every time, unless told otherwise.
Just make the default english and offer an option on the index page to change to french? This, of course, depends on what language most of the visitors speak, which isn't all that hard to figure out with visitor logs.
I would use a neutral URL for entry, such as:
http://example.com/foo/bar
On this page I would do some language negotiation or simply ask the user for the prefered language. Then I can redirect to the language specific URL:
http://example.com/en/foo/bar
what do you think about that solution
<?php
$lang = $_GET['lang'];
if ( empty($lang) ) $lang = 'fra';
header( 'Location: http://acecrodeo.com/new/01-acec.php?lang='.$lang) ;
?>