Mobile detection failing - javascript

I am trying to detect if my user is using a smartphone. I have tried so many different methods but nothing seems to detect if it is a smartphone.
The one below does not even echo $_SERVER['HTTP_USER_AGENT'];
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://mobile.site.com/');
}
?>
This is another script I tried that did not echo anything back.
<script type="text/javascript" >
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
<?php
echo "I should redirect";
?>
}
</script>
I have also tried getting the screen size, but that does not show anything.
<script type="text/javascript">
document.write(screen.width);
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
</script>
I have also tried the very popular php script below, but it also echo's nothing for $detect.
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
echo $detect;
if($detect->isMobile()) {
header('Location: http://mobile.example1.com/');
exit;
}
?>

Please check that solution:
if( strstr($_SERVER['HTTP_USER_AGENT'],'Android') ||
strstr($_SERVER['HTTP_USER_AGENT'],'webOS') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPod')
){}

Related

getting full URL in php without white spaces

I am trying to share the full URL from my website to whatsapp using mobile browser,
I tried this to retrieve the full URL and it works perfectly when I echo the variable, but when I share using the mobile browser, it gives me the link with white space like this: http://127.0.0.1/?id= 40
which can not be opened, how can I get the full echoed link without no spaces?
<?php
$alink= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ?
"https" : "http") . "://" . $_SERVER['HTTP_HOST'] .
$_SERVER['REQUEST_URI'];
?>
//To Share
<a href="whatsapp://send?text=<?php echo $message['title'] ?> <?php echo "\r\n" ?> <?php echo $alink ?> <?php echo "\r\n" ?> <?php echo $myText ?>"
data-action="share/whatsapp/share">Share To Whatsapp</a>
$alink = str_replace(' ', '', $alink)
Manual
the standard conditions [$_SERVER['HTTPS'] == 'on'] do not work on servers behind a load balancer:
$isSecure = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$isSecure = true;
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$isSecure = true;
}
$REQUEST_PROTOCOL = $isSecure ? 'https' : 'http';
echo $REQUEST_PROTOCOL."://".$_SERVER['HTTP_HOST'];
I think this will Help You and please refer PHP documentation
Get the full url by javascript with window.location.href
iny my file.php
...
<div id="url"></div>
...
<script type="text/javascript">
document.querySelector('#url').innertHTML = window.location.href
</script>

I have trouble redirecting all traffic not coming from tripadvisor to another website (WordPress)

I try to redirect all traffic that is not coming from tripadvisor to another website using php session and javascript on a wordpress website
I need help to correct my script
here is the non working script I developed using php session:
<?php
if (!isset($_SESSION)) {
session_start();
}
if (!isset($_SESSION['referrer'])) {
$_SESSION['referrer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'n/a';
}
$referer = isset($_SESSION['referrer']) ? $_SESSION['referrer'] : null;
?>
<script type="text/javascript">
var str = "<?php echo $referer ?>";
var oktogo = str.includes("tripadvisor");
if (oktogo== false) {
window.location.href = ‘https://www.mySecondWebsite.com’;
}
</script>
Hello guys I've corrected the script myself thanks for your help
Here is the script for people having the same needs
if (!isset($_SESSION)) {
session_start();
}
if(isset($_SERVER['HTTP_REFERER']) && substr_count($_SERVER['HTTP_REFERER'],"tripadvisor") == 1){
$_SESSION['is_refered_from_tripadvisor'] = true;
}
if(!isset($_SESSION['is_refered_from_tripadvisor']) || $_SESSION['is_refered_from_tripadvisor'] == false){
header("Location: https://www.mySecondWebsite.com");
}

Redirecting pages according to the user account type OR using username & password

I have been working on this PHP code, but I am not able to connect them to their specific pages using account type Dean, Registrar, Incharge of Accounts and Cashier.
What I tried:
<?php include("header.php");
include("banner.php") ?>
<?php
if (isset($_POST['btnlogin'])) {
$uname = trim($_POST['uname']);
$upass = trim($_POST['pass']);
$h_upass = sha1($upass);
if ($uname == '' OR $upass == '') {
}
?>
?> <script type="text/javascript">
alert("Invalid Username and Password!");
</script>
<?php
} else {
$user = new User();
$res = $user::AuthenticateUser($uname, $h_upass);
?>
Have you tried using header function ?
also if you can use middleware to redirect the user to specific file that will be nice practice.
<?php include("header.php");
include("banner.php") ?>
<?php
if (isset($_POST['btnlogin'])) {
$uname = trim($_POST['uname']);
$upass = trim($_POST['pass']);
$h_upass = sha1($upass);
if ($uname == '' OR $upass == '') {
}
?>
?> <script type="text/javascript">
alert("Invalid Username and Password!");
</script>
<?php
} else {
$user = new User();
$res = $user::AuthenticateUser($uname, $h_upass);
if($res)
{
if($user->role == "Dean")
{
header('Location: Dean.php');
}else if($user->role == "Dean")
{
header('Location: Registrar.php');
}
}
}
?>

Show/hide Div when select option value

When echo inside value the javascript onchange doesnt work. it must click/choose again to show the div.
<?php
$test = '1';
?>
<select name="request" id="reqtypev" class="form-control1" >
<option value="<?php echo $test ?>" selected="selected"><?php echo $test ?></option>
</select>
it does work if you didnt echo it using php.
//javascript
$('#reqtypev').change(function(){
if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
$("#otherTypev").show()
} else {
$("#otherTypev").hide()
}
});
how is it possible to execute the javascript when you echo using php ?
Trigger change event after page load for #reqtypev as following:
(function ($) {
$(function () {
$('#reqtypev').change(function(){
if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
$("#otherTypev").show()
} else {
$("#otherTypev").hide()
}
});
$('#reqtypev').trigger("change");
});
}(jQuery));
Try this :
<?php
$test = '1';
?>
<select name="request" id="reqtypev" class="form-control1" onchange="intializeDrop(this.value);" >
<option value="<?php echo $test ?>" selected="selected"><?php echo $test ?></option>
</select>
Js code:
function intializeDrop(id){
if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
$("#otherTypev").show()
} else {
$("#otherTypev").hide()
}
}
Try this
//javascript
$(document).on('change','#reqtypev',function(){
if( $(this).val()==="1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
$("#otherTypev").show()
} else {
$("#otherTypev").hide()
}
});
Hmm, I don't think it's doable in PHP, PHP executes once and quits (Until another refresh occurs)
You can do it in PHP, once. But not live where people are moving the selection bar around.
Can you put more info on what you want to do? I assume from the little code you are showing is to hide a field in the form once a person chooses a value < 1 or > 4
Value in option at least 2 values function change() to activity
<div id="otherTypev">texttttt</div>
<select name="request" id="reqtypev" class="form-control" >
<?php
for($i = 0;$i<=10;$i++){
echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
}
?>
</select>
Js
<script>
$('#reqtypev').change(function(){
if( $(this).val()=== "1" || $(this).val()==="2" || $(this).val()==="3" || $(this).val()==="4" ){
$("#otherTypev").show()
} else {
$("#otherTypev").hide()
}
});
</script>

Mobile to Desktop redirection condition doesn't work

I couldn't find anywhere PHP version of redirection depending on screen size so I needed to use combined method echo-ing JS script from PHP but the code I found in one of the answers here doesn't work for some reason. All looks fine but the link keeps going back to mobile version regardless. My desktop version link is (sorry can't display tags here):
href="http://www.webaddress.co.uk/?v=desktop" data-ajax="false" rel="external"
And at the top of my index.php desktop version I have:
<?php session_start();
if (isset($_GET['v']) && $_GET['v'] == 'desktop') {
$_SESSION['v'] == 'desktop';
}
if (!isset($_SESSION['v']) || (isset($_SESSION['v']) && $_SESSION['v'] != 'desktop')) {
echo "<script> if (window.screen.width < 1000) {window.location = '/mobile'; }</script>";
}
?>
For some reason the redirection finds the second condition always valid.
In your first if statement I'm pretty sure you want to set $_SESSION['v'] and not compare it against 'desktop'.
<?php session_start();
if (isset($_GET['v']){
if ($_GET['v'] == 'desktop') {
$_SESSION['v'] = 'desktop';
}
if ($_GET['v'] == 'mobile') {
$_SESSION['v'] = 'mobile';
}
}
if (!isset($_SESSION['v'])) {
echo "<script> if (window.screen.width < 1000) {window.location = '/?v=mobile'; } else {window.location = '/?v=desktop' }</script>";
}
?>

Categories