I'm trying to get a little script working. It shouldn't be hard, but I'm kind of a n00b when it comes to PHP (my last experiments were many years ago).
So, basically, I want a little script that IF the client uses a mobile device, it gives a link to Facebook as an app link. IF the client has a regular laptop, the script should output a regular Facebook.
Okay, so I'm just going to show my screwed up code, to make it (hopefully) more clear what I'm trying (some might notice that I grabbed part of the code from another thread):
<?php
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'mobile' ) {
if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
echo "fb://groups/334257489999204";
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
echo "fb://groups/123456789";
}
}
else {
echo "https://www.facebook.com/groups/123456789";
};
echo "https://www.facebook.com/groups/123456789";
};
?>" />
This code is in a html anchor href tag, inside a .php file (mainly HTML though).
Thanks!
This should set you on the right path
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( empty($user_agent) ) {
$is_mobile = false;
} elseif ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap|phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
$is_mobile = true;
} else {
$is_mobile = false;
}
echo (is_mobile) ? 'I am a mobile !' : 'I am not a mobile :(';
Related
I've wrote this code but it could not reading. I don't know What's happened .
I've wrote script as PHP download files using cURL. But I've an error (500).
My question is how I get the error above?
<?php
class Download {
const URL_MAX_LENGTH = 2000;
// Clean URL
protected function cleanUrl($url) {
if (isset ( $url )) {
if (! empty ( $url )) {
if (strIen ( $url ) < self::URL_MAX_LENGTH) {
return strip_tegs ( $url );
}
}
}
}
// Method Is Url
protected function isUrl($url) {
$url = $this->cleanUrl ( $url );
if (isset ( $url )) {
if (filter_var ( $url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED )) {
return $url;
}
}
}
// Retur Extension
protected function returnExtension($url) {
if $this->$isUrl ( $url ) {
$end = end ( preg_split ( "/[.]+/", $url ) );
if (issed ( $end )) {
return $end;
}
}
}
// Final Code For Download FIle
public function downloadFile($url) {
if ($this->isUrl ( $url )) {
$extension = $this->returnExtension ( $url );
}
}
}
?>
<form action="index.php" method="post">
Enter Url FOr DOwnload: <br /><input type="text" name="url" maxlength="2000" ><br />
<input type="submit" value="Download File">
</form>
you get the 500 error because PHP errors out, because php errors out during compilation, because if $this->$isUrl ( $url ) { in the returnExtension is a syntax error. replace it with if ($this->$isUrl ( $url ) ) {
and because display_errors is disabled in your php.ini, so errors are not shown to the client. if log_errors is enabled, it should be in your error logs tho, learn how to check those.
Your code is full of typos and syntax errors. The error logs, or even the error message you see when trying to run the code would tell you this. Please check your logs and error msgs before posting to Stack Overflow.
strIen() -> strlen()
strip_tegs() -> strip_tags()
if $this->$isUrl ( $url ) { -> Two problems! Firstly isUrl is a method, not a variable, so you want to call it like $this->isUrl($url). Secondly you need parenthesis around your condition, so if ($this->isUrl($url)) {.
issed() -> isset()
Also, you are using isUrl() like a test, but it does not behave like one. It simply returns a URL, sometimes. Your code expects it to return true/false instead.
I'm trying to submit user information to a URL using GET, and then get the errors (if there any) and use them to tell the customer what went wrong. So, currently I have a form that submits this customer info into an iframe (so the page is not redirected and I can see the response from my shopping cart software). when the info is submitted, this is the response I get from the shopping cart server:
errorFound=1&responseCode=329&...etc.
I need to get this response code, and was wondering what the most simple way would be to do it. Once I get it I can tell the customer what the problem is... Should I use java to read the
data in the iframe once it loads? or can I use something like Fopen to open the URL and get the return data (can't enable fopen on my server though, but something like it?).
Java != javascript
A quick way to do it:
$errorcodes = array("329" => "Wrong blabla");
if( isset( $_GET['errorFound'] ) && isset( $_GET['responseCode'] ) ){
$errorNr = (int) $_GET['responseCode'];
$error = getErrorFromDB();
//OR
//$error = isset( $erorCodes[ $errorNr ] )? $errorcodes[ $errorNr] : false;
if( $error !== false){
exit( "<script type='text/javascript'>
alert( '".htmlspecialchars($error)."' )
</script>");
}
}
function getError( $code )
{
$code = (int) $code;
$db = getYourPdoInstance();
$q = $db->prepare("SELECT `message` FROM `errorCodes` WHERE `errorCode` = ?");
$q->execute( array( $code) );
$return = $q->fetch(2);
return isset($return['message'])?$return['message']:false;
}
I have to two site one is my main site and otherone is for mobile site.This is the script i am using for redirecting the site on mobile when it is being used in mobile device. Now i want to ignore the mobile site redirection for iPad. I have used this script but its not ignoring iPad it still redirecting on mobile site on Ipad and i dont want this. Plz help.
<?php
function check_user_agent ( $type = NULL ) {
$user_agent = strtolower ( $_SERVER['HTTP_USER_AGENT'] );
if ( $type == 'bot' ) {
// matches popular bots
if ( preg_match ( "/googlebot|adsbot|yahooseeker|yahoobot|msnbot|watchmouse|pingdom\.com|feedfetcher-google/", $user_agent ) ) {
return true;
// watchmouse|pingdom\.com are "uptime services"
}
} else if ( $type == 'browser' ) {
// matches core browser types
if ( preg_match ( "/mozilla\/|opera\//", $user_agent ) ) {
return true;
}
} else if ( $type == 'mobile' ) {
// matches popular mobile devices that have small screens and/or touch inputs
// mobile devices have regional trends; some of these will have varying popularity in Europe, Asia, and America
// detailed demographics are unknown, and South America, the Pacific Islands, and Africa trends might not be represented, here
if( preg_match ( "/iPad/", $user_agent )) {
return false;
} else if ( preg_match ( "/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", $user_agent ) ) {
// these are the most common
return true;
} else if ( preg_match ( "/mobile|pda;|avantgo|eudoraweb|minimo|netfront|brew|teleca|lg;|lge |wap;| wap /", $user_agent ) ) {
// these are less common, and might not be worth checking
return true;
}
}
return false;
}
$ismobile = check_user_agent('mobile');
if($ismobile) {
header('Location:mobiles_site_url');
}
?>
You used strtolower() on the user agent string and the first line to check for the 'iPad' has an uppercase letter in it.
Try:
if( preg_match ( "/ipad/", $user_agent )) { // all lower case
....
}
Why does the code below work great on most websites (such as www.apple.com), but doesn't work for others (such as www.yahoo.com)?
$tags = get_meta_tags( $webpage );
if( $tags )
{
if( $tags['description'] )
{
$link_description = $tags['description'];
}
else
{
$link_description = '';
}
}
When I say that it doesn't work, what I mean is that the script just stalls. My program is left hanging. Is there a way to catch when get_meta_tags() doesn't work and handle this with an alternative block of code? I thought I was doing this when I wrote
$tags = get_meta_tags( $webpage );
if( $tags )
{
// stuff
}
else
{
// handle the case when the above doesn't work.
}
However, it still stalls on sites like www.yahoo.com. Any ideas on how to handle these troublesome URL's?
Yesterday, i post an 'ad' on an ebay site (marktplaats.nl) and the ad was immediately visible in google when i search for this ad on Google.
How do they do this?
Because it is a very large site, i don't think they repost the sitemap. I have made such function to update the sitemap but don't know this is a good method. What i have is this (part of my library):
// returns false when input invalid, returns a number (array index) when url is invalid or // request fails, returns true when complete:
function suSubmitSitemap( $sXmlUrl, $asSearchEnginePingUrl = 'http://www.google.com/ping?sitemap=%s', $bContinueOnError = false )
{
if( !suIsValidString( $sXmlUrl ) || ( !suIsValidString( $asSearchEnginePingUrl ) && !suIsValidArray( $asSearchEnginePingUrl )))
{ return false; }
$a = (is_array($asSearchEnginePingUrl))?$asSearchEnginePingUrl:explode(',', $asSearchEnginePingUrl );
$sXmlUrl = urlencode( $sXmlUrl );
$ret = false;
foreach( $a as $i=>$sUrl )
{
$sUri = str_replace( '%s', $sXmlUrl, $sUrl );
$bValid = (!is_bool( strpos( $sUrl, '%s' )) && suGetUrlContent( $sUri ));
if( !$bValid )
{
if( !$bContinueOnError )
{ return $i; }
if( !is_array( $ret ))
{ $ret = array(); }
$ret[$i] = $sUri;
}
}
return ret;
}
Is this a safe way to do this (Google will not ban you when frequently called), when this is not the way to do it, does anyone know how implement such index update functionality in PHP?
Google will constantly crawl frequently updated/popular sites and update their search results. This has the benefit of making Google more relevant.
Resubmitting your site map will not help you get crawled as fast as Ebay. Get more visitors and post content more frequently to get Google to visit your site more often.
Also, check out:
http://www.google.com/support/webmasters/bin/answer.py?answer=48620
http://www.searchenginejournal.com/10-ways-to-increase-your-site-crawl-rate/7159/