I have read a lot of articles and looked for solutions to detect mobile devices. Actually a came across https://github.com/serbanghita/mobile-detect but it's a quite massive php class.
I actually want a very simple solution. I want to determine if the user's browser is Mobile/iPad/etc OR Desktop. So I want something like this:
<?php
require('detector.php');
if(isMobile() === true)
{
header('mobile.php');
exit();
}
else
{
header('desktop.php');
exit();
}
?>
A very simple solution is needed which I can place to any page without installing composer or any php framework.
How is this actually possible?
Have you actually tried to use the project you discovered. I'd say that server side mobile detection IS a huge task with plenty of detail checks to ensure the correct outcome.
And using this class is completely easy. From the example directory:
require_once '../Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
Now you have a variable with one of three values: "tablet", "phone" or "computer", and you can react to this.
Please note that even if you are able to use this library without Composer, it will be updated regularly (as in "once every month"), because new devices get on the market and need to be detected. You will have to update this library at some point. Using Composer makes this very easy.
If you really don't want to include that class into your code, Mozilla indicates that it is "good enough" to search for the string "mobi" in the user agent.
<?php
if (stristr($_SERVER['HTTP_USER_AGENT'],'mobi')!==FALSE) {
echo 'mobile device detected';
}
?>
You can redirect the link with
and in controller you can check with
$keybord = app::get('keyboard')
if($keyboard == mobile ){
redirect ('mobile');
}else{
redirect ('desktop');
}
I've found this simple line to be pretty reliable and easy to implement.. without having the need to add one extra class.
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
echo "running on mobile";
}
Related
Hi everybody and tks in advance for your help!
I have a multilengual site made in a similar way to this example. Everything is working just fine, but now I want to make that the URL change according to the languague selected. For example, if my page is called perfil.php when I select english languague, should be profile.php, and all the links in the web should translate to english too. I was surfing another questions but the majority offers a solution through htaccess. This should work, but I need to store that configuration (or translations) into my database so the user can change it when they want to.
Any ideas?
Thank you again!
You can use constants in some files called language files and then require/include them by language selected:
english.php
const PROFILE = "profile";
spanish.php
const PROFILE = "perfil";
main file:
require $language_selected . ".php";
echo '<a href="' . PROFILE . '.php">';
This part should be a comment (but its a bit long)
The method described in the link you provided is a reasonable way to implement the choice of language, but a poor way to detect the choice.
Your browser already tells servers what language(s) it thinks they should respond in. And most webservers have a mechanism for multiplexing different language content. However the latter means hard-wiring the choice of the browser without providing an easy means for overriding the behaviour.
The approach I have used before is something like this:
$use_lang='en-GB';
if (isset($_COOKIE['userlang'])
&& is_language_supported($_COOKIE['userlang'])) {
$use_lang=$_COOKIE['userlang'];
} else if ($proposedlang=supported_lang_in($_SERVER['Accept-Language'])) {
$use_lang=$proposedlang;
}
function supported_lang_in($str)
{
$l=array();
$opts=explode(',', $str);
foreach ($opts as $v) {
list($p, $weight)=explode(';', $v);
if ($weight) {
list($dummy, $weight)=explode('=', $weight);
$weight=float($weight);
}
if (!$weight) {
$weight=1.0;
}
if (isset($l[$weight])) {
$weight-=0.001;
}
$l[$weight]=$p;
}
krsort($p); // preferred first
foreach ($p as $proposed) {
if ('*'==$proposed) {
return false;
}
if (is_language_supported($proposed)) {
return $lang;
}
}
return false;
}
Now on to the problem you asked about....
Maintaining different URLs to reference the same content then dereference the language within the content seems a very byzantine solution to the problem. Not only do you have to map the input to the URL but you need to rewrite any URLs in the output to the appropriate representation.
While having semantically meaningful URLs is a definite bonus, going to great length to tailor these dynamically is not perhaps not the best use of your time.
I have a project that requires <a href="tel:123456"> over the phone number (123456 is not the number) however this will cause an issue for everything except mobile browsers by being unable to interpret the tel: bit.
I have tried to use jQuery to add the href attr when a browser width is <1000px but this is a very unreliable method (tablets also have a limited resolution).
Any ideas on how to overcome this would be greatly appreciated. I am using PHP, jQuery and JavaScript.
Detect with PHP if its a mobile agent:
http://www.000webhost.com/forum/scripts-code-snippets/27404-php-extremely-simple-way-check-if-mobile-browser.html
<?php
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
echo '<a href="tel:123456">';
}else{
echo 'You are not in a mobile';
}
?>
Since you're using PHP I can recommend the php-mobile-detect class. You can cater to individual devices/OS'es/browsers, or simply use isMobile() or isTablet() as a catch-all.
I usually do something like this:
include './includes/Mobile_Detect.php';
$detect = new Mobile_Detect;
if ( $detect->isMobile() or $detect->isTablet() ){
$phone='1-234-567-8910';
} else {
$phone='1-234-567-8910';
}
Then I just <?php echo $phone;?> wherever I need it and it works a treat! And by using PHP instead of javascript it means the detection is done server-side, so the end user has less to download (like jQuery and extra scripts).
The library of devices gets updated fairly often, so it's worth checking the GitHub page every so often.
Try this below one :
Syntex : callto
9566603286
<?php
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
echo '9566603286';
}else{
echo 'You are not in a mobile';
}
?>
I need a login to let 10 students to view educational material. Simple is good.
Perhaps it just redirects to a page if student logs in correctly. Can you send me a link or example, or best tutorial?
I was told JavaScript alone doesn't work, so the next simplest thing is preferred.
If there's an example where I don't have to rename all of my pages 'php', that would be better.
Thanks,
I used this when I was learning to do secure logon using PHP.
http://www.devshed.com/c/a/PHP/Creating-a-Secure-PHP-Login-Script/1/
Found it quite helpful.
Simple...
Create a file called functions and insert the following:
session_start();
$_GLOBALS['users'] = array(
//'username' => 'password'
'robert' => 'my_pass'
);
function isAuthed()
{
if(empty($_SESSION['logged_in']))
{
if(!empty($_REQUEST['username']) || !empty($_REQUEST['password']))
{
if(isset($_GLOBALS['users']) && is_array($_GLOBALS['users']))
{
if(isset($_GLOBALS['users'][$_REQUEST['username']]) && $_GLOBALS['users'][$_REQUEST['username']] === $_REQUEST['password'])
{
$_SESSION['logged_in'] = true;
return true;
}
}
}
}else
{
return true;
}
return false;
}
and then in your secured pages just do:
if(!isAuthed())
{
die("You're not authorized to see this page");
}
and on your login page just create a form that sends the username, password to the an area of your site that your authorizing
Note: This is not copy and past'able code, this is for example purposes only.
You could possibly "do" it with JavaScript if you did some kind of AJAX function which called a php page, and then returned your value. This could work, and it's how a lot of sites do their logins actually. So, your client wouldn't have to rename their site, and you could just set up an array of logins on the php page.
This would NOT be secure at all, but it would work just fine.
I guess you would do something like this (I'm going to use jQuery because it's easier to do Ajax with it. It's really easy to use, and if you're going to learn Javascript, it's probably better nowadays to know the basics and then use a framework library like jQuery)
$(document).ready(function(){
$("#NAME-OF-SUBMIT-BUTTON").submit(function(){
var username = $(this).find("#username");
var password = $(this).find("#password");
$("NAME-OF-DIV-FOR-RETURN").load('login.php', {['parameters']:,[username,password]},function(responseText){
if(responseText == 'SUCCESSFUL-RESPONSE-TEXT'){
$("#NAME-OF-FORM").html("Login Successful");
}
});
});
});
and of course you're going to want to set a session variable or cookie or something on your php page to indicate the user has logged in. Again, this is not very secure, but it's what I would do if it were like a homework assignment or just SUPER temporary. Of course, I would suggest making hard-coded usernames and passwords in an array on your original page in PHP with a postback to itself if you were going to go that temporary. Using Javascript and Ajax for this just seems like a bit much.
But, you requested it!
It depends on exactly what you're trying to do. If you want a pure-JS login system, you could do something fairly simple like XOR'ing a redirect page with a password, storing that in the page and then XOR'ing it again when they type in a password.
If you want an actual login-system, you need a back-end running some server (perhaps Node.js if you're trying to learn JavaScript), some type of database (e.g. MySQL), users stored in that database.
The front-end javascript might be responsible for validating the login via ajax. Using jQuery, something like:
function validateLogin(user, pass, successCallback, errorCallback){
$.get('/login', {user: user, pass:pass}, function(data){
if(data.status == 'success'){
successCallback();
}else{
errorCallback();
}
}
}
I want to control the access in php website.
I have a solution right now with switch case.
<?php
$obj = $_GET['obj'];
switch ($obj)
{
case a:
include ('a.php');
break;
default:
include ('f.php');
}
?>
But when i have so many pages, it becomes difficult to manage them. Do you have better solutions?
Right now, i develop the application using php4. And i want to use php5. Do you have any suggestions when i develop it using php5?
Thanks
$obj = $_GET['obj'];
$validArray = array('a','b','c','d','e');
if (in_array($obj,$validArray)) {
include ($obj.'.php');
} else {
include ('f.php');
}
The more pages you have the harder it will be to control this.
Your best off using a framework of some sort, my personal preference is CodeIgniter.
why not just address a page itself?
first page
another page
I am not saying that this is the best solution, but years ago I used to have a website which used a database to manage the key, the page to be included, and some informations like additional css for instance.
So the code was something like:
<?php
$page = htmlspecialchars($_GET['page']);
$stuffs = $db->query('select include,css from pages where pageid = "' . $page . '" LIMIT 1');
?>
So when we needed to add a page, we just created a new field in the database. That let us close a part of the website too: we could have a "available = {0,1}" field, and if zero, display a static page saying that this page was under maintenance.
I just saw http://uservoice.com/login. It uses Google accounts, Myspace, Yahoo, OpenID and all to sign in customers into its site? Can I do that?
I mean, customers need not register to my site. They can just sign in with their accounts on the above sites.
If you've a solution, I'd prefer a PHP and MySQL based one.
See here: Google Login PHP Class.
Also be sure to refer to the Google Federated Login site for more info.
You may want to look at this too: https://rpxnow.com/ - it will only need integrating at the HTML/javascript level.
It's what http://uservoice.com/login appears to use.
You should look at the OpenID Enablded PHP library (http://www.openidenabled.com/php-openid/).
This should play pretty nicely with any LAMP installation without needing to use Zend.
Zend_OpenId from Zend Framework
Zend_OpenId is a Zend Framework component that provides a simple API for building OpenID-enabled sites and identity providers.
http://openidenabled.com/php-openid/
Uservoice users RPX http://rpxnow.com . You can easily use it with PHP, just https and parse the json or xml repsonse. You don't even need to change your database schema or store anything locally.
i think is good solution for you
step by step
1-download openid
2-create file called login.php like this (in same directory or change require_one to your own ) :
<?php
require_once 'openid.php';
$myopenid = new LightOpenID("your-domain.com");//no problem even if u can write http://localhost
if ($myopenid->mode) {
if ($myopenid->mode == 'cancel') {
echo "User has canceled authentication !";
} elseif($myopenid->validate()) {
$data = $myopenid->getAttributes();
$email = $data['contact/email'];
$first = $data['namePerson/first'];
echo "Identity : $openid->identity <br>";
echo "Email : $email <br>";
echo "First name : $first";
} else {
echo "The user has not logged in";
}
} else {
echo "Go to index page to log in.";
}
?>
3-next is about creating file called index.php:
<?php
require_once 'openid.php';
$openid = new LightOpenID("your-domain.com");//no problem even if u can write http://localhost
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array(
'namePerson/first',
'namePerson/last',
'contact/email',
);
$openid->returnUrl = 'your-domain.com/login.php'
?>
Login with Google
i almost forgot for log out u can kill session;