I'm having an issue with this PHP browser-specific alert - php

Im currently coding a design tutorial site for developers and I have a snippet of code that basically throws a small alert on the page if the end-user is using an outdated version of IE on the screen.
As useless as it sounds, it's what the client wants. Oh well.
Basically, I can't get the damn this to close when you click the "Close this box" link. Here is the code. Any suggestions?
<?php
// IE6,7,8 string from user_agent
$ie6 = "MSIE 6.0";
$ie7 = "MSIE 7.0";
$ie8 = "MSIE 8.0";
// detect browser
$browser = $_SERVER['HTTP_USER_AGENT'];
// yank the version from the string
$browser = substr("$browser", 25, 8);
// html for error
$error = "<div class=\"error\" id=\"error\"><strong>Alert:</strong> It appears that you
are using Internet Explorer 6, 7, or 8. While you may still visit this website we
encourage you to upgrade your web browser so you can enjoy all the rich features this
website offers as well as other websites. Follow this link to <a
href=\"http://www.microsoft.com/windows/downloads/ie/getitnow.mspx\"><strong>Upgrade
your Internet Explorer</strong></a><br /><div class=\"kickRight\"><a href=\"javascript:
killIt('error');\"> Close This Box</a></div></div>";
// if IE6 set the $alert
if($browser == $ie6){ $alert = TRUE; }
if($browser == $ie7){ $alert = TRUE; }
if($browser == $ie8){ $alert = TRUE; }
?>
And then you add this into the BODY wherever you may want it:
<!-- IE6 Detect Advise Notice -->
<?php if($alert){ echo $error; } ?>
<!-- // end IE6 Detect Advise Notice -->
I can't get the damn this to close. I don't know what the problem is. It may be the there is javascript trying to close a PHP error box. I don't know c-based languages so I don't know. Any help is appreciated

Your close button is attempting to fire a JavaScript function called killIt() with a parameter of 'error'. I'm going to guess that you haven't included that function on the page, or that there's an error in it.

Just add the killIt javascript function to HTML. It would not remove any PHP code at server-side, but rather HTML output of PHP code in the browser. You haven't shown us the code for killIt, but my guess is that it either hides or removes the DIV that PHP prints.

Related

Simple PHP code not working on mobile

THis may be super basic, but I have not been able to resolve this after spending hours!
I am running PHP 7 on Ubuntu 16.1.
The PHP file is EXACTLY as follows
<?php
header("Content-type: application/javascript");
header("HTTP/1.1 200 OK");
ExpandShortLink();
function ExpandShortLink()
{
// get URL
$URL_To_Expand = $_REQUEST['url'];
// for short links, get the full links
// get full URL
$arr_URL_Header = get_headers($URL_To_Expand, 1);
$strLink = $arr_URL_Header['Location'];
//echo $URL_To_Expand;
//print_r($arr_URL_Header);
if ($strLink) {
if (is_array($strLink)) {
$Full_URL = array_pop($strLink);
} else {
$Full_URL = $strLink;
}
} else {
$Full_URL = $URL_To_Expand;
}
echo $Full_URL;
}
--> produces the url I enter as a "url" parameter on desktop. But nothing on mobile!
On some reading, I found that in sometimes PHP interprets everything after "//" as a comment and that may be happening here. But then why does it happen on mobile only? Also, andy suggestions on resolving this will be great!
Thanks much for your help,
You can see this live here
If you click this on desktop, you will see http:// example. com. However, on mobile it will return http:
Not sure if this qualifies as answer, but I wanted to put a note here for anyone else who may be facing a similar problem.
I was using the PHP pasted above for an ajax call. I tried using text/plain instead of application/json and now it works across all browsers and all devices (as far as I could test).
Not sure why application/javascript was causing problems on mobile chrome, but I think text/plain makes sense as I was just passing back a text string instead of a javascript.
As I said it probably is not the fully qualified answer, but hopefully it helps someone in future!

Disable a PHP Plugin on mobile Screens with Code?

I have this plugin called "WP FLOAT", I want to disable it when it detects a small screen size, lets say smaller than 400px. Is there a line of PHP code I can paste in one of the plugin's files to make this happen?
These are the only files for the plugin:
wp-float/wp-float(.)php
wp-float/readme(.)txt
wp-float/js/jquery.hoverIntent.minified(.)js
wp-float/js/jquery.easing(.)js
wp-float/js/wp-float-button(.)js
wp-float/js/jquery.floater.2.2(.)js
wp-float/js/button-wpfloat(.)php
What would I paste and in which file? Thanks
As Nicolas said, there's no way to access the client screen size on server side unless we have previously sent that information from the client side, JavaScript for example.
But, headers brings us a nice information. So, if you're looking to disable it for, let's say, mobile devices, which we can assume they won't have a big screen, you can try reading the headers of the request to get the browser's agent, lets say, in PHP:
if (preg_match('/iphone|android|blackberry|nokia/i'),$_SERVER['HTTP_USER_AGENT']))
echo "Looks like a small screen device...";
You could also try this PHP class for detecting mobile devices:
http://code.google.com/p/php-mobile-detect/
Later you can do something like this
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if ($detect->isMobile()) {
echo "It's a phone or tablet";
}
if($detect->isTablet()){
echo "It's a tablet";
}
if ($detect->isMobile() && !$detect->isTablet()) {
echo "It's a phone ;)";
}
well you can remove properly with jquery after you get the current size of screen or with media querys in css.
I don't think you can do that, because PHP is on the server, there is no way to get the size of the screen from it.
However, you could use javascript to redirect to the current page and disable the plugin:
if(!isset( $_GET['r'] ) )
{
$script = "<script>";
$script .= 'document.location="' . $_SERVER['PHP_SELF']. '?r=1&width=screen.width';
$script .= "</script>";
echo $script;
}
else {
if( isset( $_GET['width'] ) && $_GET['width'] < 400 )
// Disable plugin
}
I would suggest you to change the plugin or look for another workaround, because I believe this is gonna be a little slow.

How can I not load a JS script on a certain OS client?

I am using the WYSIWYG editor CKEditor which apparently does not get along with Windows Phone 8 at all (it breaks the entire site when kept in the head). I'm not certain if it's an IE issue overall or Win8 specifically, but the site will be accessed via a custom corp app wrapper so suggesting an alternate browser isn't an option. I don't really care about them losing the functionality.
Is there a simple way I can write something like this into the head in PHP?
if(OS == Windows Phone 8){
//nothing
} else {
echo "<script src=\"ckeditor/ckeditor.js\"></script>";
}
I'm open to any solution, but PHP is what I'm using in general.
You can check the user agent in PHP and include the script if it doesn't match Windows Phone 8:
$pattern = "/Windows\sPhone\s8/";
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if(!preg_match($pattern, $user_agent))
{
echo "<script src=\"ckeditor/ckeditor.js\"></script>";
}
If it's IEMobie 10 causing the problem you can check for that in the Useragent.
if(stristr($_SERVER['HTTP_USER_AGENT'], 'IEMobile/10.0') === FALSE)
echo '<script src="ckeditor/ckeditor.js"></script>';
You could even do it in Javascript if you prefer:
if(navigator.userAgent.indexOf('IEMobile/10.0') === -1)
document.getElementsByTagName('head')[0].innerHTML += '<script src="ckeditor/ckeditor.js"></script>';
You can also use conditional comments:
<![if !IEMobile]>
<script src="ckeditor/ckeditor.js"></script>
<![endif]>
Since you're using javascript, why not match the user agent using javascript instead of PHP? [Credit]
if !(navigator.userAgent.match(/IEMobile\/10\.0/)) { ...
*On Windows 7 phone you could target the browser using just HTML, but unfortunately Microsoft got rid of that feature with Windows Phone 7.5.

Browser Detection with Javascript

Reasoning for this Question
I am aware that browser detection can never be 100% reliable as the User Agent header can always be forged, however, I am not bothered by this.
Although there are many questions on this topic, they all seem to be quite old, so to get an up to date answer I felt I should ask this question again.
I am currently detecting the browser name and version server side using the PHP browscap, and then returning the name and version into javascript variables (not a very good method). The reason why I need to do this is simply to display a message to visitors if they are not using a supported browser.
Current method (something similar):
<script type="text/javascript">
var browser = new Array();
browser['browser'] = '<?php echo $browser_name; ?>';
browser['version'] = '<?php echo $browser_version; ?>';
browser['error'] = '<?php echo $browser_error; ?>';
</script>
It would be much better to do this client side as the browscap can be quite slow, and it would prevent me having to pass values into javascript variables from PHP. If you think using PHP is a better method then please state in your answer, this is just my opinion.
Question
Therefore, my question quite simply, is the following link a reliable method for determining the browser name and version?
Javascript Detect
I am aware that new browsers will need to be added to this, this does not bother me. I am more concerned about whether the algorithm used is reliable.
Thanks in advance
UPDATE 1
To see what I mean, take a look at https://www.icloud.com/ in Internet Explorer 7 or less. You will receive a message saying that the browser is not supported. This is easy to do for IE as you can simply use the <!--[if gt IE..., however, I need to test all browsers.
This does not look right, you can fetch browser information from Javascript. No need to mix JS and PHP code to do that.
You can do something like this to fetch, and detect user browser with just JavaScript:
var userAgent = navigator.userAgent.toLowerCase();
var old = false;
// Internet Explorer 7
if (userAgent.indexOf('msie 7.0b') !== -1) { old = true; }
else if (userAgent.indexOf('msie 7.0') !== -1) { old = true; }
// Internet Explorer 6
else if (userAgent.indexOf('msie 6.1') !== -1) { old = true; }
else if (userAgent.indexOf('msie 6.01') !== -1) { old = true; }
else if (userAgent.indexOf('msie 6.0b') !== -1) { old = true; }
else if (userAgent.indexOf('msie 6.0') !== -1) { old = true; }
...
// Detect any other browser versions you consider old
if(old = true) {
// Show notification and alert users that they are using old browser
}
This is how you can do it using JS, but you can also use HTML to achieve this:
<!--[if lte IE 6]>
// include your .css style or do whatever you want to alert users their browser is old
<![endif]-->
Short answer to your question is YES, its wrong to detect user browser the way you do it, since you can do it with plain JavaScript, or even with HTML. No need to mix PHP and JS code here, and at the end, both PHP and JS will get the same UserAgent info.
Explanation
After extensive research and discussing amongst other developers, it is clear that there is no reliable method for retrieving the browser name and version from the User Agent. This is down to several reasons:
The format of a browsers User Agent can change at any time if the developers of the browser so wish to do so. This could immediately prevent some scripts from working correctly.
Users can forge their User Agents to mimic other browsers, and therefore would appear to be using a browser they are not.
Possible Solutions
Whilst I hugely discourage the use of these scripts as they could stop working at the release of an update to any browser anytime, if you do wish to detect the browser name and version in Javascript then I would advise using this script:
Javascript Detect
However, the most reliable method for retrieving the details of the browser is without a doubt the browscap supplied by Gary Keith. The browscap project offers extensive information about each browser and OS gathered from the User Agent. It is very easy to implement and even easier to use. To read more, take a look at:
Gary Keith - Browscap
If you choose to use the browscap by Gary Keith, you will need to ensure it is updated weekly at the very least.
Answer
Whilst I am contradicting myself with this answer, it is clear that detecting the browser information with any sort of script is not advised. The only reliable method of browser detection is that of the Internet Explorer HTML conditions, and as stated, these only cover Internet Explorer.
Try to avoid browser specific functions and notices, and make use of the built in features such as:
media="only screen and (device-width: 768px)"
and
<!--[if IE 8]>I am IE 8<![endif]-->
This question needs an updated answer. I think the best option these days for client-side detection is WURFL.
Its an updated library of devices based on Useragents - think Browscap for the client side.
Load the JS and it returns JSON based on the device that requested the js. Perfect!
<script type="text/javascript" src="//wurfl.io/wurfl.js"></script>
Because it does the parsing on the WURFL server side, you need to load the js remotely and not save it in your dir tree.
A super easy
WURFL.is_mobile
is all it takes to determine mobile for example.
Good luck.
You could try having a look at navigator.appName and navigator.userAgent.
The yepnopejs IE detection (!ie prefixes) works by utilizing the MS conditional comments.
A short snippet for detecting versions of IE prior to IE10 in JavaScript without resorting to user-agent sniffing.
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->'
);
// …
https://github.com/SlexAxton/yepnope.js/blob/master/prefixes/yepnope.ie-prefix.js
yepnope usage example:
yepnope({
load: ['normal.js', 'ie6!ie7!ie-patch.js'] // patch for ie6 or ie7 only
});
You can use a perfect plugin for this information written in jQuery (like javascript)
look at this link:
https://github.com/jquery/plugins.jquery.com
Be sure to do feature detection instead of browser detection when you want to determine if a certain feature is available in a browser, apply bugfixes, etc.

php crawler detection

I'm trying to write a sitemap.php which acts differently depending on who is looking.
I want to redirect crawlers to my sitemap.xml, as that will be the most updated page and will contain all the info they need, but I want my regular readers to be show a html sitemap on the php page.
This will all be controlled from within the php header, and I've found this code on the web which by the looks of it should work, but it's not. Can anyone help crack this for me?
function getIsCrawler($userAgent) {
$crawlers = 'firefox|Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|' .
'AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|' .
'GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
$isCrawler = (preg_match("/$crawlers/i", $userAgent) > 0);
return $isCrawler;
}
$iscrawler = getIsCrawler($_SERVER['HTTP_USER_AGENT']);
if ($isCrawler) {
header('Location: http://www.website.com/sitemap.xml');
exit;
} else {
echo "not crawler!";
}
It looks pretty simple, but as you can see i've added firefox into the agent list, and sure enough I'm not being redirected..
Thanks for any help :)
You have a mistake in your code:
$crawler = getIsCrawler($_SERVER['HTTP_USER_AGENT']);
should be
$isCrawler = getIsCrawler($_SERVER['HTTP_USER_AGENT']);
If you develop with notices on you'll catch these errors much more easily.
Also, you probable want to exit after the header
Warning: Cloaking can get you in trouble with search providers. This article explains why.
http://develobert.blogspot.com/2008/11/php-robot-check.html

Categories