Disable a PHP Plugin on mobile Screens with Code? - php

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.

Related

How to get, in php, the entire html of a page loaded in part from jquery

i've this problem for days...
I have to load from php the entire html of a page.
On this page there is a jquery function that is called when all the page is loaded. This function loads other html into page, so i have to get all the html loaded ( the part loaded with jquery too). I can know that i get all the page trying to find some tag loaded only from jquery. ( for example: tag input with name XXX, tag input with attribute multiple, etc. )
so i try:
$html = file_get_contents("http://wwww.siteToScrape.com");
if (strpos($html, 'multiple') !== false) {
echo 'found';
} else {
echo 'not found';
}
but result is 'not found'.
Then i downloaded simple html dom and i try:
include 'simple_html_dom.php';
$html = file_get_html("http://wwww.siteToScrape.com");
if (strpos($html, 'multiple') !== false) {
echo 'found';
} else {
echo 'not found';
}
but result still remain 'not found'.
so i think to get some php script what emulate browser ( so can load jquery too ) and i downloaded PHP Scriptable Web Browser and i try:
require_once('browser.php');
$browser = new SimpleBrowser();
$p = $browser->get('http://wwww.siteToScrape.com');
if (strpos($p, 'multiple') !== false) {
echo 'found';
} else {
echo 'not found';
}
but result is still again 'not found'.
I don't know how to do it.. can someone help me??? thanks!!!!
The problem is that you are trying to mix server and client.
PHP runs on the server
Javascript (and therefor also jQuery) runs in the client browser.
There's no easy way to run the javascript using PHP. As far as I know, it's not even possible. Other languages, such as Java might be able to do what you are trying to do.
You should look at another way to do this.
This is also the reason why webcrawlers never gets affected by stuff you do using javascript. This is a nice thing to keep in mind when developing. Your dynamic loading will not be indexed by these crawlers at all.
As far as I know, this is not possible "with only PHP". Javascript runs on the client instead of the server and therefore it would not be possible without some sort of a browser emulator environment.
Edit: You could put javascript in the web page itself which would fetch the innerHTML of the whole web page after it was fully generated and then use an ajax call to send that to your server. You would have to stay within the limitations of the same-origin-policy (which doesn't allow you to make ajax calls to domains other than where the host web page came from).
Like the others have said, jquery is javascript, and is typically executed by the client (web browser) rather than the server.
PHP, being a server-side language, has no javascript interpreter.
The easiest way that I know of to run javascript using PHP is via web-testing tools, which often integrate a headless browser. You could check out mink, which has a back-end for the zombie node.js headless browser.
There's also the phantomjs headless browser with various PHP interfaces like this one, which I found with a quick google search.
In the more resource-intensive arena, there's also selenium, which has PHP interfaces as well.

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

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.

How to use a javascript function in a pure php page

I am looking for a way to include a .js function with in a straight php page. By straight php I mean there is no html included.
Explanation of process if you will.
I have a page where employees must swipe their ID badge for access to a computer.
The employee swipes the badge, the magnetic strip is read, and the data sting is sent to the db to get the access levels etc. This works great unless I get a bad card read. What I have is a .js file that pulls the ID and the issue date of the badge from the data string and validates it before going to the db. If it fails it errors.
At the error point I will ask them to swipe the card again etc...
So back to the top, can I use this .js file in a php file.
If not, can someone point me to a library or chart where I can find the comparison values for js and php. (.js - var s = ""; | .php $s = ""; etc...)
Can't you implement the validation logic in php:
<?php
function validateId($id = null) {
// your validation code goes here
if($id != null) {
// Code to be executed for a successful swipe
echo "success";
} else {
// Code to be executed for a failed swipe
echo "An epic failure occurred. Please swipe again";
}
}
I think your best bet would be to install a server-side JS engine and then run it using system.
A reasonable example is Narwhal. If you install that on your server, then you can do something like this from PHP:
system('js /path/to/my/js/file.js',$retval);
The only thing you may have to modify is any case where your JS interacts with the browser (i.e. document.writes, DOM manipulation), but Narwhal has equivalents for interacting with the CLI.

php load page page into div ,java script not working

i am using ajax to load pages into a div
the page is loading fine
but i cant run the php and javascript
in that loaded page
in server i am loading the page like this
file_get_contents('../' . $PAGE_URL);
in the browser i am setting the content of the div
using
eval("var r = " + response.responseText);
and setting the innerHTML for that div
with the retrieve information
but when i get the new inner page
no php or java script is working
is that suppose to be like that ?
Well the php is not going to work I think because the way you are handling it, it is just text. I would suggest using something like include('../' . $PAGE_URL); and that should parse the php.
The javascript problem probably has to do with the fact that you are loading <html> <body> <head> tags in a div I'm not sure what happens when you do that, but it shouldn't work properly. Try using some type of <frame> tag.
In order for your javascript to be executed properly, you have to wait until the browser has finished to load the page.
This event is named onload(). Your code should be executed on this event.
<?php
$file = false;
if(isset($_GET['load'] && is_string($_GET['load'])) {
$tmp = stripclashes($_GET['load']);
$tmp = str_replace(".","",$tmp);
$file = $tmp . '.php';
}
if($file != false && file_exists($file) && is_readable($file)) {
require_once $file;
}
?>
called via file.php?load=test
That process the PHP file, and as long as you spit out HTML from the file simply
target = document.getElementById('page');
target.innerHTML = response.responseText;
Now, i'm fairly certain parts of that are insecure, you could have a whitelist of allowable requires. It should ideally be looking in a specific directory for the files also. I'm honestly not all too sure about directly dumping the responseText back into a DIV either, security wise as it's ripe for XSS. But it's the end of the day and I haven't looked up anything on that one. Be aware, without any kind of checking on this, you could have a user being directed to a third party site using file_get_contents, which would be a Very Bad Thing. You could eval in PHP a file_get_contents request, which... is well, Very Very Bad. For example try
<?php
echo file_get_contents("http://www.google.com");
?>
But I fear I must ask here, why are you doing it this way? This seems a very roundabout way to achieve a Hyperlink.
Is this AJAX for AJAXs sake?

redirecting browser to a new image, with content type image/png in PHP

I have a php script which generates an image, and is used (mainly) like this:
<img src="user_image.php?id=[some_guid]" />
The script uses a class I wrote to display an image matching that ID. There are a number of things that could go wrong though, and each of them throws an exception. So I have something like this:
<?php
try {
if( ! isset($_GET['id']) ) throw new Exception;
$images = new User_Images;
$images->display($_GET['id']);
} catch( Exception $e ) {
header('location: images/link_error.png');
}
If I view this from the browser, everything is fine -- if there was an error the address in the address bar changes to images/link_error.png and displays that instead.
But when this script is used in an <img> tag, and there is an error grabbing the image, it doesn't show up at all.
Do header redirects not work this way? What is another way that I can do this?
update
There is no problem, browser redirects work perfectly this way, the issue was that my browser was caching the empty image that was returned before the redirect was put in. A hard refresh (Ctrl + F5 for Firefox) fixed it and it started working like normal.
Hummm... My guess is that the
header('Location: images/link_error.png');
Because headers were previously sent?
Try placing an ob_start() on the top of your file and see if it solves your problem.
Here is a simple way to debug this:
try {
if (!isset($_GET['id'])) throw new Exception;
$images = new User_Images;
$images->display($_GET['id']);
} catch (Exception $e) {
if (headers_sent() === false)
{
header('Location: images/link_error.png'); // also try using the absolute URL here
}
else
{
echo file_get_contents('http://www.google.com/intl/en_ALL/images/logo.gif');
}
}
If the Google logo shows up, you need to trace where you're outputting data or use ob_start() + ob_end_clean().
this tag is really nice!
Couldn't you just give the error image a default ID (i.e. zero) and use $images->display(0)? Then the error image is handled exactly like the successful case.
Interesting - I would expect a call to a <img> tag to work with a redirect. Anyway, it might not. I'd be interested to hear why not. Anyone?
The easiest workaround that comes to mind is passing through the error image using fopen() and fpasshtru().
A lot of places like PhotoBucket just display an error image when there are problems loading the requested one. This is usually a pre-constructed image that just says "Image Error" or whatever. When images are being put together dynamically, there aren't many other options than this other than returning a 404.

Categories