Search:

Categories



Get zodiac star sign for a date

 <?php
function zodiac($DOB){
    $DOB = date("m-d", strtotime($DOB));
    list($month,$day) = explode("-",$DOB);
    if(($month == 3 || $month == 4) && ($day > 22 || $day < 21)){
        $zodiac = "Aries";
    }
    elseif(($month == 4 || $month == 5) && ($day > 22 || $day < 22)){
        $zodiac = "Taurus";
    }
    elseif(($month == 5 || $month == 6) && ($day > 23 || $day < 22)){
        $zodiac = "Gemini";
    }
    elseif(($month == 6 || $month == 7) && ($day > 23 || $day < 23)){
        $zodiac = "Cancer";
    }
    elseif(($month == 7 || $month == 8) && ($day > 24 || $day < 22)){
        $zodiac = "Leo";
    }
    elseif(($month == 8 || $month == 9) && ($day > 23 || $day < 24)){
        $zodiac = "Virgo";
    }
    elseif(($month == 9 || $month == 10) && ($day > 25 || $day < 24)){
        $zodiac = "Libra";
    }
    elseif(($month == 10 || $month == 11) && ($day > 25 || $day < 23)){
        $zodiac = "Scorpio";
    }
    elseif(($month == 11 || $month == 12) && ($day > 24 || $day < 23)){
        $zodiac = "Sagittarius";
    }
    elseif(($month == 12 || $month == 1) && ($day > 24 || $day < 21)){
        $zodiac = "Cpricorn";
    }
    elseif(($month == 1 || $month == 2) && ($day > 22 || $day < 20)){
        $zodiac = "Aquarius";
    }
    elseif(($month == 2 || $month == 3) && ($day > 21 || $day < 21)){
        $zodiac = "Pisces";
    }

    return $zodiac;
}
//usage example
echo zodiac('1976-12-06'); //Valid strtotime date
echo "<br>";
echo zodiac('1986-04-04');
?> 

Whois in PHP

This is a basic whois example

A good list of whois servers can be found at http://www.nirsoft.net/whois_servers_list.html

<?php
function whois_query($domain) {

    // fix the domain name:
    $domain = strtolower(trim($domain));
    $domain = preg_replace('/^http:\/\//i', '', $domain);
    $domain = preg_replace('/^www\./i', '', $domain);
    $domain = explode('/', $domain);
    $domain = trim($domain[0]);

    // split the TLD from domain name
    $_domain = explode('.', $domain);
    $lst = count($_domain)-1;
    $ext = $_domain[$lst];

    // You find resources and lists 
    // like these on wikipedia: 
    //
    // http://de.wikipedia.org/wiki/Whois
    //
    $servers = array(
        "biz" => "whois.neulevel.biz",
        "com" => "whois.verisign-grs.com",
        "us" => "whois.nic.us",
        "coop" => "whois.nic.coop",
        "info" => "whois.nic.info",
        "name" => "whois.nic.name",
        "net" => "whois.internic.net",
        "gov" => "whois.nic.gov",
        "edu" => "whois.internic.net",
        "mil" => "rs.internic.net",
        "int" => "whois.iana.org",
        "ac" => "whois.nic.ac",
        "ae" => "whois.uaenic.ae",
        "at" => "whois.ripe.net",
        "au" => "whois.aunic.net",
        "be" => "whois.dns.be",
        "bg" => "whois.ripe.net",
        "br" => "whois.registro.br",
        "bz" => "whois.belizenic.bz",
        "ca" => "whois.cira.ca",
        "cc" => "whois.nic.cc",
        "ch" => "whois.nic.ch",
        "cl" => "whois.nic.cl",
        "cn" => "whois.cnnic.net.cn",
        "cz" => "whois.nic.cz",
        "de" => "whois.nic.de",
        "fr" => "whois.nic.fr",
        "hu" => "whois.nic.hu",
        "ie" => "whois.domainregistry.ie",
        "il" => "whois.isoc.org.il",
        "in" => "whois.ncst.ernet.in",
        "ir" => "whois.nic.ir",
        "mc" => "whois.ripe.net",
        "to" => "whois.tonic.to",
        "tv" => "whois.tv",
        "ru" => "whois.ripn.net",
        "org" => "whois.pir.org",
        "aero" => "whois.information.aero",
        "nl" => "whois.domain-registry.nl"
    );

    if (!isset($servers[$ext])){
        die('Error: No matching nic server found!');
    }

    $nic_server = $servers[$ext];

    $output = '';

    // connect to whois server:
    if ($conn = fsockopen ($nic_server, 43)) {
        fputs($conn, $domain."\r\n");
        while(!feof($conn)) {
            $output .= fgets($conn,128);
        }
        fclose($conn);
    }
    else { die('Error: Could not connect to ' . $nic_server . '!'); }

    return $output;
}
//usage example
echo whois_query("http://www.yahoo.com");
echo "<br>";
echo whois_query("http://www.yahoo.net");
?>

Seconds to string

submitted by PHPhacker

<?php
function secondsToString($secs) 
{
    if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
    if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
    if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
    $r.=$secs.' second';if($secs<>1){$r.='s';}
    return $r;
}

//usage examples
echo secondsToString(5237);
echo "<br>";
echo secondsToString(88238);
?>

Base64 Encode and Decode String

<?php
function base64url_encode($sampleText) 
{
    $base64 = base64_encode($sampleText);
    $base64url = strtr($base64, '+/=', '-_,');
    return $base64url;
}
 
function base64url_decode($sampleText) 
{
    $base64url = strtr($sampleText, '-_,', '+/=');
    $base64 = base64_decode($base64url);
    return $base64;
} 

//usage example
echo base64url_encode("sample text"); //output = c2FtcGxlIHRleHQ,
echo "<br>";
echo base64url_decode("c2FtcGxlIHRleHQ,");
?>

user authentication

 <?php
session_start();
$vu = array('user1' => 'password1', 'user2' => 'password2');

function doAuth() {
    header('WWW-Authenticate: Basic realm="User Authentication"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'You cannot view this page.';
    exit;    
}

function checkUser() {
    global $vu;
    $b = false;
    if($_SERVER['PHP_AUTH_USER']!='' && $_SERVER['PHP_AUTH_PW']!='') {
        if($vu[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']) $b = true;
    }
    return $b;
}

if(!checkUser()) doAuth();

// The rest of the page
?> 

Convert web page to pdf

Uses a 3rd party service, more can be found out at http://pdfcrowd.com

<?php
// convert a web page
require 'pdfcrowd.php'
$client = new Pdfcrowd("your-username", "your-apikey");
$client->convertURI('http://www.example.com/', fopen('example.pdf', 'wb'))
?>