Friday, December 17, 2010

Get data from other site in php

$html = file_get_contents("http://www.website.com/blog/");
now print_r the $html and you will find all the content on your page


To see a specific part
preg_match_all (  '/< li >.*?< h1 >< a href=" (. *? ) ">(. *? )< \/ a ><\/ h1 >.*?(. *?)<\/s pan>.*?(.*?)<\/di v>.*?<\/ li>/ s',
    $html,
    $posts, // will contain the blog posts
    PREG_SET_ORDER // formats data into an array of posts
);
for each $posts as $post)

{
    $link = $post[1];
    $title = $post[2];
    $date = $post[3];
 $content = $post[4];

    // do something with data
}

echo the variable in foreach loop




How to Hide error console in browser

make a function in javascript and write
window.Onerror=true;

Now console error not shows on browser

Saturday, November 20, 2010

javascript date difference in php

function checkdate()
{
t1="1/1/2006" ;

t2="25/1/2006";

//Total time for one day
var one_day=1000*60*60*24;
//Here we need to split the inputed dates to convert them into standard format
var x=t1.split("/");
var y=t2.split("/");
//date format(Fullyear,month,date)
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;
//Calculate difference between the two dates, and convert to days
Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
document.write(Diff);
//_Diff gives the diffrence between the two dates.
}

Thursday, November 18, 2010

easy url rewriting

this is code for url rewriting

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://localhost/url/download.php?id=$1-$2 [NC]


this is link in address bar you have to type

http://localhost/blog/2003-nov

Friday, October 1, 2010

call Rss feed in php


function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->textContent;

$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->textContent;

$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->textContent;

$y["title"] = $title;
$y["link"] = $link;
$y["description"] = $description;
$y["type"] = $type;

return $y;
}


function RSS_Channel($channel)
{
global $RSS_Content;

$items = $channel->getElementsByTagName("item");

// Processing channel

$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);

// Processing articles

foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}

function RSS_Retrieve($url)
{
global $RSS_Content;

$doc = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
RSS_Channel($channel);
}

}


function RSS_RetrieveLinks($url)
{
global $RSS_Content;

$doc = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}

}

}


function RSS_Links($url, $size = 15)
{
global $RSS_Content;

$page = "
    ";

    RSS_RetrieveLinks($url);
    if($size > 0)
    $recents = array_slice($RSS_Content, 0, $size + 1);

    foreach($recents as $article)
    {
    $type = $article["type"];
    if($type == 0) continue;
    $title = $article["title"];
    $link = $article["link"];
    $page .= "
  • $title
  • \n";
    }

    $page .="
\n";

return $page;

}



function RSS_Display($url, $size = 15, $site = 0)
{
global $RSS_Content;

$opened = false;
$page = "";
$site = (intval($site) == 0) ? 1 : 0;

RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, $site, $size + 1 - $site);

foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="\n";
$opened = false;
}
$page .="";
}
else
{
if($opened == false)
{
$page .= "
    \n";
    $opened = true;
    }
    }
    $title = $article["title"];
    $link = $article["link"];
    //$description = $article["description"];
    $page .= "
  • $title";
    if($description != false)
    {
    $page .= "
    $description";
    }
    $page .= "
  • \n";

    if($type==0)
    {
    $page .="

    ";
    }

    }

    if($opened == true)
    {
    $page .="
\n";
}
return $page."\n";

}


?>

$url="http://www.referd.info/tag/php/rss.php";
echo RSS_Display($url, 15);
?>

generate random number and catcha code in php

function Random()
{
$chars = "ABCDEFGHJKLMNPQRSTUVWZYZ7589641230";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 4)
{
$num = rand() % 32;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}

?>

http to https in php

if ($_SERVER['SERVER_PORT']!=443)
{
$url = "https://". $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
header("Location: $url");
}

select all check box in php

script type="text/javascript">
checked=false;
function checkedAll (frm1) {
var aa= document.getElementById('frm1');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
/script>

form id ="frm1"
input type="checkbox" name="chk1"
input type="checkbox" name="chk2"
/form>

Check curl is on php

function Visit($url)

{

$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();

curl_setopt ($ch, CURLOPT_URL,$url );

curl_setopt($ch, CURLOPT_USERAGENT, $agent);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch,CURLOPT_VERBOSE,false);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$page=curl_exec($ch);

//echo curl_error($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if($httpcode>=200 && $httpcode<300) return true;

else return false;

}

if(Visit("http://localhost"))

echo "Website OK"."n";

/*if site down*/

else

echo "Website DOWN";