Monday, December 30, 2013

Common security functions in PHP





<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * IsEmpty
 * 
 * @param mixed $value
 * @return boolean
 */
function IsEmpty($value) {
    if(strlen(trim(preg_replace('/\xc2\xa0/',' ',$value))) == 0) {
        return true;
    }else {
        return false;
    }
}


?>




<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * Sanitize
 * 
 * @param mixed $value
 * @return string
 */
function Sanitize($value) {
    if(get_magic_quotes_gpc()) $value = stripslashes($value);
    return htmlentities($value, ENT_QUOTES, 'utf-8');
}


?>





<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ParseId
 * 
 * @param integer $id
 * @param integer $segment
 * @return mixed
 */
function ParseId($id, $segment = 2) {
    $pattern = '/^([0-9]+)-';
    for($i = 2; $i < $segment; $i++) {
        $pattern .= '([0-9]+)-';
    }
    $pattern .= '([0-9]+)$/';
    if(!preg_match($pattern, $id, $match)) {
        return false;
    }else {
        return $match;
    }
}


?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidEmail
 * 
 * @param string $email
 * @return boolean
 */
function ValidEmail($email) {
    if(preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2,4}$/i", $email)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidURL
 * 
 * @param string $email
 * @return boolean
 */
function ValidURL($url) {
    if(preg_match('/^https?:\/\/[^\s]+/i', $url)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


/**
 * ValidString
 * 
 * @param string $string
 * @return boolean
 */
function ValidString($string) {
    if(preg_match('/^[a-z0-9A-z ]*$/i', $string)) {
        return true;
    }else {
        return false;
    }
}


?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ValidUserName
 * 
 * @param string $string
 * @return boolean
 */
function ValidUserName($string) {
    if(preg_match('/^[a-z0-9]*$/i', $string)) {
        return true;
    }else {
        return false;
    }
}

?>
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

/**
 * ValidImg
 * 
 * @param string $file
 * @return boolean
 */
function ValidImg($file) {
    $extensions = array('jpg','gif','png');
    $len  = strLen($file)-3;
    $ext  = strtolower(substr($file, $len, 3));
    if(in_array($ext, $extensions)) {
        return true;
    }else {
        return false;
    }
}


?>

Social Media URLs and PHP and JQuery


In this post we are learn check popularity of URL across social network like Facebook, Twitter, Reddit, Google plus, StumbleUpon, LinkedIn and Pinterest. Popularity means URL  how many times shared ,liked ,commented and pinned by the users across the social networks.

Note:  Use CURL function to grab this purpose. 

Facebook:

https://graph.facebook.com/fql?q=SELECT+like_count%2C+total_count%2C+share_count%2C+click_count%2C+comment_count+FROM+link_stat+WHERE+url+%3D+%22http%3A%2F%2Fgoogle.co.in%2F%22


It returns:

{
   "data": [
      {
         "like_count": 58263,
         "total_count": 211166,
         "share_count": 125191,
         "click_count": 86,
         "comment_count": 27712
      }
   ]
}






Twitter:

https://cdn.api.twitter.com/1/urls/count.json?url=http%3A%2F%2Fgoogle.co.in


It returns:

{"count":762,"url":"http:\/\/google.co.in\/"}









Reddit:

http://www.reddit.com/api/info.json?url=http%3A%2F%2Fgoogle.co.in

It returns:

{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "technology", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "100fzk", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "marktripeasy", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_2qh16", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/technology/comments/100fzk/httplocalhost42600loyaltyaccountpointsummary/", "name": "t3_100fzk", "created": 1347867646.0, "url": "http://google.co.in", "author_flair_text": null, "title": "http://localhost:42600/Loyalty/Account/PointSummary", "created_utc": 1347864046.0, "ups": 1, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "AdviceAnimals", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "s40co", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "ayushrj", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2s7tt", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/AdviceAnimals/comments/s40co/google/", "name": "t3_s40co", "created": 1334131500.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "Google", "created_utc": 1334127900.0, "ups": 1, "num_comments": 1, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "funny", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "7zkbq", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "mbbala", "media": null, "score": 0, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2qh33", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 1, "saved": false, "is_self": false, "permalink": "/r/funny/comments/7zkbq/my_first_page/", "name": "t3_7zkbq", "created": 1235393963.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "My First Page", "created_utc": 1235393963.0, "ups": 1, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "todayilearned", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "hv9ap", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "turner13", "media": null, "score": 0, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2qqjc", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 2, "saved": false, "is_self": false, "permalink": "/r/todayilearned/comments/hv9ap/til_the_guitar_was_invented_by_les_paul_by/", "name": "t3_hv9ap", "created": 1307598634.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "TIL the Guitar was invented by Les Paul by visiting the Google Home Page today. There is a Guitar on Google Home page which plays music too..", "created_utc": 1307595034.0, "ups": 2, "num_comments": 3, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "science", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "1170xo", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "moneykannan", "media": null, "score": 0, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_mouw", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 6, "saved": false, "is_self": false, "permalink": "/r/science/comments/1170xo/search/", "name": "t3_1170xo", "created": 1349793735.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "SEARCH", "created_utc": 1349790135.0, "ups": 3, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "ExploreAmerica", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "13bj9u", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "moheetarora", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_2umqx", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/ExploreAmerica/comments/13bj9u/googling/", "name": "t3_13bj9u", "created": 1353101986.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "googling", "created_utc": 1353101986.0, "ups": 1, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "politics", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "zfvol", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "barackromney", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_2cneq", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/politics/comments/zfvol/barackromney_yes_we_can_together_believe_in/", "name": "t3_zfvol", "created": 1346922086.0, "url": "http://google.co.in", "author_flair_text": null, "title": "BarackRomney: \"Yes We can Together Believe in America\"", "created_utc": 1346918486.0, "ups": 1, "num_comments": 1, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "sex", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "f01o3", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "lmyadav", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2qh3p", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/sex/comments/f01o3/hhh/", "name": "t3_f01o3", "created": 1294724871.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "hhh", "created_utc": 1294724871.0, "ups": 1, "num_comments": 0, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "Twitter", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "bh817", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "hruaia", "media": null, "score": 1, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "default", "subreddit_id": "t5_2qhwg", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 0, "saved": false, "is_self": false, "permalink": "/r/Twitter/comments/bh817/google/", "name": "t3_bh817", "created": 1269371573.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "Google", "created_utc": 1269367973.0, "ups": 1, "num_comments": 1, "visited": false, "num_reports": null, "distinguished": null}}, {"kind": "t3", "data": {"domain": "google.co.in", "banned_by": null, "media_embed": {}, "subreddit": "reddit.com", "selftext_html": null, "selftext": "", "likes": null, "secure_media": null, "link_flair_text": null, "id": "dx2to", "secure_media_embed": {}, "clicked": false, "stickied": false, "author": "hrkindian", "media": null, "score": 0, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "", "subreddit_id": "t5_6", "edited": false, "link_flair_css_class": null, "author_flair_css_class": null, "downs": 6, "saved": false, "is_self": false, "permalink": "/r/reddit.com/comments/dx2to/google/", "name": "t3_dx2to", "created": 1288174192.0, "url": "http://www.google.co.in", "author_flair_text": null, "title": "Google", "created_utc": 1288170592.0, "ups": 2, "num_comments": 3, "visited": false, "num_reports": null, "distinguished": null}}], "after": null, "before": null}}



StumbleUpon:

http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http%3A%2F%2Fgoogle.co.in

It returns:

{"result":{"url":"http:\/\/www.google.co.in\/","in_index":true,"publicid":"1sbqjf","views":50621,"title":"Google","thumbnail":"http:\/\/cdn.stumble-upon.com\/mthumb\/849\/280849.jpg","thumbnail_b":"http:\/\/cdn.stumble-upon.com\/bthumb\/849\/280849.jpg","submit_link":"http:\/\/www.stumbleupon.com\/submit\/?url=http:\/\/www.google.co.in\/","badge_link":"http:\/\/www.stumbleupon.com\/badge\/?url=http:\/\/www.google.co.in\/","info_link":"http:\/\/www.stumbleupon.com\/url\/www.google.co.in\/"},"timestamp":1388407808,"success":true}





LinkedIn:


http://www.linkedin.com/countserv/count/share?url=http%3A%2F%2Fgoogle.co.in&format=json

It returns:

{"count":2586,"fCnt":"2,586","fCntPlusOne":"2,587","url":"http:\/\/google.co.in"}




Pinterest:


http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url=http%3A%2F%2Fgoogle.com

It returns:

receiveCount({"count": 10615, "url": "http://google.com"})


 

© 2014 4everTutorials. All rights resevered.

Back To Top