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';
}
?>
Related
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";
}
I'm developing a website which has a mobile counterpart. I have some code at the top of the page which detects if mobile, and redirects using the php header() command accordingly. I'm now looking at adding an option to 'switch to desktop version' on the mobile site, but then stay desktop.
I'm guessing I need to create a session variable named something like 'mobile' but I can't quite work out where I should place it.
Any advice would be amazing. Below is the code one each page. I am not positing the mobile_detect.php as it's rather long winded, but can be found at http://mobiledetect.net
<?php require_once('functions/Mobile_Detect.php');
$detect = new Mobile_Detect();
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() ){
header("location:mobile/index.php");
}
?>
try
if ( !isset($_SESSION['forceDesktop']) )
$_SESSION['forceDesktop'] = false;
$detect = new Mobile_Detect();
// Exclude tablets.
if( $detect->isMobile() && !$detect->isTablet() && !$_SESSION['forceDesktop'] ){
header("location:mobile/index.php");
}
....
// some logic asking whether to force the desktop version
....
$_SESSION['forceDesktop'] = true;
How can I validate my form without using JavaScript? I have used the required HTML 5 attribute but as IE doesn't support IE (ANNOYING) is there a way around this with using PHP which will take time.
Thanks
You should always validate on the server side (e.g., with PHP). There are probably a lot of libraries that will make this easier and save you some time, but the principle's pretty simple:
if (isset($_POST['submit'])) {
$name = isset($_POST['name']) ? $_POST['name'] : null;
}
$errors = '';
if (!$name or strlen($name) < 5) {
$errors .= 'Name must not be empty and at least five characters';
}
if ($errors) {
echo $errors;
}
else {
//Store data from the form or whatever you want to do.
}
I would also recommend using Post/Redirect/Get
You can use PHP to validate using the server:
http://thinkvitamin.com/code/php-form-validation/
Only server side validation (ie in PHP) or using javascript. Although, I'd go for the server side part - it is the safest, since a bad user could forge bad data from the client side
I have a website build using PHP and HTML. If a user browses my website using IE, I want to display a message saying: "Please use Firefox or Google Chrome" instead of rendering the index page.
Is it possible? If so how could it be done?
Please note I am not that advanced with PHP.
Thanks.
You could also do it like this:
<?php
function using_ie(){
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ub =false;
if(preg_match('/MSIE/i',$user_agent))
{$ub = true;}
return $ub;
}
if(using_ie()==true){
//Show notice
}else{
//Cont
}
?>
Dont forget that that IE users still own 30% of the market share meaning 1 in 3.3 of your users will be using IE http://www.w3counter.com/globalstats.php
I will give you the best answer
include this code in <head>
<!--[if IE]>
<script>
alert("Here you can write the warning box like this website is not supported by IE");
</script>
<script type="text/javascript">
window.location = "insert here the link of the webpage you want to redirect the users after clicking ok";
</script>
<![endif]-->
You can do that with Conditional Comments which are documented by the browser vendor (Microsoft).
With those you can make HTML accessible to IE users that are hidden in comments in every other standards compliant browser, like the message to download some other browser. You can even completely hide the rest of the page.
It is possible, but i just want to tell you it's really not a really good solution to a problem. The way it can be done is:
$browserinfo = get_browser();
$browser = $browserinfo['browser']
if ($browser == "Internet Explorer" || $browser == "InternetExplorer" || $browser == "IE") {
include("path/to/your/errorMessage.php");
exit(0);
}
This does require browscap. Another option is:
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = false;
if(preg_match('/MSIE/i',$u_agent))
{
include("path/to/your/errorMessage.php");
exit(0);
}
Please checkout the user-agent as described here
http://icfun.blogspot.de/2008/07/php-how-to-check-useragent.html
i hope this is what your are looking for ;-)
Im doing a game and i created an "administration" panel for it.
it works like this:
admin.php
admin/code1.php
admin/code2.php
admin.php:
<?php
include("lib.php");
$player = check_user($secret_key, $db);
if($player->rank != "Administrador")
{
header("Location: personagem.php");
exit;
}
include("templates/private_header.php");
head("Admin Panel","adminpanel.png");
startbox();
if(isset($_GET['page']))
{
include("admin/" . $_GET['page'] . ".php");
}
else
{
?>
Kill Players<br>
Heal Players<br>
<?php
}
endbox();
include("templates/footer.php");
?>
i want to know if im prone to hacking.
the code1.php and code2.php uses a custom query library that is included in lib.php so there is no way to execute them directly without falling in to an error.
Also in My template i have:
if($player->rank == "Administrador")
{
echo "<a href='admin.php'>Admin Panel</a>";
}
so i can access the panel more quickly.There is risk in there too?
Just note that $player is a object created from a query to the player Database that represents the actual player. In my thoughts the only way to hack this is changing they "rank" status in the table to "Administrador" am i right? or there is something i let pass?
Thanks in advance
include("admin/" . $_GET['page'] . ".php");
This is a huge security hole.
Something like blah.php?page=../../../../etc/passwd%00 would include /etc/password and of course you can also do this with other files - maybe even some files uploaded by the user that contain PHP code (could be even an image as long as it contains <?php [code] somewhere)
And even if only you are administrator, not closing holes like that would not be wise - you might have other administrators at some point.
Never trust user input
Never work with any of $_GET $_POST, $_COOKIE without verifying them first (or anything else user-generated for that matter, even stuff from your own database might be dangerous).
include("admin/" . $_GET['page'] . ".php");
Don't do this. otherwise you can include any file you want. I suggest you whitelist all allowed pages to be included like so:
$allowed = array("admin_index", "page1", "page2");
if(in_array($_GET['page'], $allowed)){
include("admin/" . $_GET['page'] . ".php");
}
else{
// perform error handling
}
Here's a useful function that you could take a look at, if you don't want to whitelist all pages: basename() - this will always only return the filename part, without any directory-changing part.
Furthermore, I do not recommend you work with includes like this at all, but rather have some Controller-hierarchy that can decide what to do on each request.
What about the authentication?
Show us your code for the authentication. That's a crucial part of your system that needs to be secure.