<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: php code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 14 Oct 2008 05:21:31 GMT</pubDate>
    <description>DZone Snippets: php code</description>
    <item>
      <title>running total in PHP</title>
      <link>http://snippets.dzone.com/posts/show/5889</link>
      <description>// output running total&lt;br /&gt;// userID,posts,runningTotal|&lt;br /&gt;// 2,23434,28330|&lt;br /&gt;// 6,3443,4896|&lt;br /&gt;// 1,422,1453|&lt;br /&gt;// 3,344,1031|&lt;br /&gt;// 4,344,687|&lt;br /&gt;// 5,343,343|&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$q = mysql_query("select * from `members` order by `posts` DESC");&lt;br /&gt;echo 'userID,posts,runningTotal|&lt;br&gt;';&lt;br /&gt;while($a = mysql_fetch_row($q)){&lt;br /&gt;echo "$a[0],$a[1],$total|&lt;br&gt;";&lt;br /&gt;$total = $total - $a[1];&lt;br /&gt;}  &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Aug 2008 11:26:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5889</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Querystring variables</title>
      <link>http://snippets.dzone.com/posts/show/4947</link>
      <description>&lt;code&gt;&lt;br /&gt;// Add Querystring Variable&lt;br /&gt;// A PHP function that will add the querystring variable $key with a &lt;br /&gt;// value $value to $url. If $key is already specified within $url, &lt;br /&gt;// it will replace it.&lt;br /&gt;&lt;br /&gt;function add_querystring_var($url, $key, $value) {&lt;br /&gt;$url = preg_replace('/(.*)(\?|&amp;)' . $key . '=[^&amp;]+?(&amp;)(.*)/i', '$1$2$4', $url . '&amp;');&lt;br /&gt;$url = substr($url, 0, -1);&lt;br /&gt;if (strpos($url, '?') === false) {&lt;br /&gt;return ($url . '?' . $key . '=' . $value);&lt;br /&gt;} else {&lt;br /&gt;return ($url . '&amp;' . $key . '=' . $value);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Remove Querystring Variable&lt;br /&gt;// A PHP function that will remove the variable $key and its value &lt;br /&gt;// from the given $url.&lt;br /&gt;&lt;br /&gt;function remove_querystring_var($url, $key) {&lt;br /&gt;$url = preg_replace('/(.*)(\?|&amp;)' . $key . '=[^&amp;]+?(&amp;)(.*)/i', '$1$2$4', $url . '&amp;');&lt;br /&gt;$url = substr($url, 0, -1);&lt;br /&gt;return ($url);&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 05 Jan 2008 07:07:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4947</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Unicode words from online dictionary</title>
      <link>http://snippets.dzone.com/posts/show/4708</link>
      <description>// list words from unicode dictionary &lt;br /&gt;// you need to add this line in the head section&lt;br /&gt;//   &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;for ( $i = 1; $i &lt;= 45; $i++) {&lt;br /&gt;$url="http://dsal.uchicago.edu/cgi-bin/romadict.pl?page=$i&amp;table=molesworth&amp;display=utf8";&lt;br /&gt;$text=file_get_contents($url);&lt;br /&gt;$myarray = preg_match_all('#&lt;font size="\+1"&gt;(.*?)&lt;/font&gt;#i', $text, $matches);&lt;br /&gt;echo implode(' ',$matches[1]); &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 28 Oct 2007 08:55:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4708</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Password Protection</title>
      <link>http://snippets.dzone.com/posts/show/4614</link>
      <description>// Bare Bones demo of password protection&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;$password = "abc123";&lt;br /&gt;if ($_POST[doddle] == $password) {&lt;br /&gt;        $tell = 1;&lt;br /&gt;} else {&lt;br /&gt;        $tell = 2;&lt;br /&gt;}&lt;br /&gt;if (! is_string($_POST[doddle])) $tell = 0;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;title&gt;A Page that is password protected&lt;/title&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;&lt;?php if ($tell == 1) { ?&gt;&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;The page with all the secrets revealed &lt;/h1&gt;&lt;br /&gt; &lt;br /&gt;This is the information that has been revealed to you because you&lt;br /&gt;got the password right!&lt;br /&gt;&lt;br /&gt;Although this is a very simple&lt;br /&gt;demo it shows what you can do with just a few lines of PHP.&lt;br /&gt; &lt;br /&gt;&lt;?php } else { ?&gt;&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;A Header page&lt;/h1&gt;&lt;br /&gt; &lt;br /&gt;There is a page hidden under a password at this URL. For this demo only&lt;br /&gt;I will tell you that the password is "abc123" so you can try it out!&lt;br /&gt;&lt;br /&gt;&lt;?php if ($tell == 2) print ("&lt;br /&gt;YOU GOT THE PASSWORD WRONG&lt;br /&gt;"); ?&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;form method=post&gt;&lt;br /&gt;Please enter password &lt;input type=password name=doddle&gt;&lt;br /&gt;&lt;input type=submit value=go&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt; &lt;br /&gt;&lt;?php } ?&gt;&lt;br /&gt; &lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;&lt;a href=http://www.wellho.net&gt;Well House Consultants&lt;/a&gt;, 2007&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 04 Oct 2007 09:52:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4614</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>tinyurl explode</title>
      <link>http://snippets.dzone.com/posts/show/4603</link>
      <description>// check where tinyurl.com is headed to&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    &lt; ?php&lt;br /&gt;&lt;br /&gt;    // tinyurl.php?c=&lt;br /&gt;&lt;br /&gt;    $num = $_GET['c'];&lt;br /&gt;&lt;br /&gt;    if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30))&lt;br /&gt;    {&lt;br /&gt;    if ($fp) {&lt;br /&gt;    fputs ($fp, "HEAD /$num HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");&lt;br /&gt;    while (!feof($fp)) {$headers .= fgets ($fp,128);}&lt;br /&gt;    fclose ($fp);&lt;br /&gt;    }&lt;br /&gt;    $arr1=explode("Location:",$headers);&lt;br /&gt;    $arr=explode("\n",trim($arr1[1]));&lt;br /&gt;    echo trim($arr[0]);&lt;br /&gt;    }&lt;br /&gt;    ?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 02 Oct 2007 08:03:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4603</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Display all files in a directory</title>
      <link>http://snippets.dzone.com/posts/show/4367</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;?&lt;br /&gt;/**&lt;br /&gt;* Change the path to your folder.&lt;br /&gt;* This must be the full path from the root of your&lt;br /&gt;* web space. If you're not sure what it is, ask your host.&lt;br /&gt;*&lt;br /&gt;* Name this file index.php and place in the directory.&lt;br /&gt;*/&lt;br /&gt;    // Define the full path to your folder from root&lt;br /&gt;    $path = "/home/content/s/h/a/shaileshr21/html/download";&lt;br /&gt; &lt;br /&gt;    // Open the folder&lt;br /&gt;    $dir_handle = @opendir($path) or die("Unable to open $path");&lt;br /&gt; &lt;br /&gt;    // Loop through the files&lt;br /&gt;    while ($file = readdir($dir_handle)) {&lt;br /&gt; &lt;br /&gt;    if($file == "." || $file == ".." || $file == "index.php" )&lt;br /&gt; &lt;br /&gt;        continue;&lt;br /&gt;        echo "&lt;a href=\"$file\"&gt;$file&lt;/a&gt;&lt;br /&gt;";&lt;br /&gt; &lt;br /&gt;    }&lt;br /&gt;    // Close&lt;br /&gt;    closedir($dir_handle);&lt;br /&gt;?&gt; &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 29 Jul 2007 05:52:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4367</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Alternating Row Colors</title>
      <link>http://snippets.dzone.com/posts/show/4278</link>
      <description>// tutorial from http://www.webreference.com/programming/php_color/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php  &lt;br /&gt;// TEST1: FOR - Control  &lt;br /&gt;for ($k = 0; $k &lt; 10000000; $k++) {  &lt;br /&gt;$class = (1) ? "bg1" : "bg2";  &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;// TEST2: FOR - Remainder division  &lt;br /&gt;for ($k = 0; $k &lt; 10000000; $k++) {  &lt;br /&gt;$class = ($k % 2 == 1) ?"bg1" : "bg2";  &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;// TEST3: FOR - Negation  &lt;br /&gt;$b = true;  &lt;br /&gt;for ($k = 0; $k &lt; 10000000; $k++) {  &lt;br /&gt;$class = ($b) ? "bg1" : "bg2";  &lt;br /&gt;$b = !$b;  &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;// TEST4: WHILE - Control  &lt;br /&gt;$k = 0;  &lt;br /&gt;while ($k &lt; 10000000) {  &lt;br /&gt;$class = (1) ? "bg1" : "bg2";  &lt;br /&gt;$k++;  &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;// TEST5: WHILE - Remainder division  &lt;br /&gt;$k = 0;  &lt;br /&gt;while ($k &lt; 10000000) {  &lt;br /&gt;$class = ($k % 2 == 1) ?"bg1" : "bg2";  &lt;br /&gt;$k++;  &lt;br /&gt;}  &lt;br /&gt;&lt;br /&gt;// TEST6: WHILE - Negation  &lt;br /&gt;$k = 0;  &lt;br /&gt;$b = true;  &lt;br /&gt;while ($k &lt; 10000000) {  &lt;br /&gt;$class = ($b) ? "bg1" : "bg2";  &lt;br /&gt;$b = !$b;  &lt;br /&gt;$k++;  &lt;br /&gt;}  &lt;br /&gt;?&gt;  &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 08 Jul 2007 14:47:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4278</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Validate email and domain</title>
      <link>http://snippets.dzone.com/posts/show/4022</link>
      <description>// Vlidate email and domain name&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;function validate_email($email){&lt;br /&gt;&lt;br /&gt;$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";&lt;br /&gt;&lt;br /&gt;if(eregi($exp,$email)){&lt;br /&gt;	if(checkdnsrr(array_pop(explode("@",$email)),"MX")){&lt;br /&gt;		print("$email is ok.&lt;br&gt;");&lt;br /&gt;			}else{&lt;br /&gt;			print("$email is ok. But domain is not.&lt;br&gt;");&lt;br /&gt;			}&lt;br /&gt;				}else{&lt;br /&gt;				print("$email is not ok.&lt;br&gt;");&lt;br /&gt;				}   &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;validate_email("shantanu.ok");&lt;br /&gt;validate_email("shantanu.ok@gmail.com");&lt;br /&gt;validate_email("shantanu.ok@fsjaldkfjlsfjsljflsfjsldk.com");&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 May 2007 07:48:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4022</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>FTP to get a file from within PHP</title>
      <link>http://snippets.dzone.com/posts/show/3619</link>
      <description>&lt;code&gt;&lt;br /&gt;$conn_id = ftp_connect("www.yoursite.com");&lt;br /&gt;$login_result = ftp_login($conn_id, "username", "password");&lt;br /&gt;&lt;br /&gt;if ((!$conn_id) || (!$login_result)) {&lt;br /&gt;echo "FTP connection has failed!";&lt;br /&gt;exit;&lt;br /&gt;} else {&lt;br /&gt;echo "Connected";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// get the file&lt;br /&gt;$local = fopen("local.txt","w");&lt;br /&gt;$result = ftp_fget($conn_id, $local,"httpdocs/trlog.txt", FTP_BINARY);&lt;br /&gt;&lt;br /&gt;// check upload status&lt;br /&gt;if (!$result) {&lt;br /&gt;echo "FTP download has failed!";&lt;br /&gt;} else {&lt;br /&gt;echo "Downloaded ";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// close the FTP stream&lt;br /&gt;ftp_close($conn_id);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 Mar 2007 04:32:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3619</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>Yubnub "echoshortcut" to display text stored for alias</title>
      <link>http://snippets.dzone.com/posts/show/872</link>
      <description>&lt;?php&lt;br /&gt;&lt;br /&gt;if (!$link = mysql_connect('', '', '')) {&lt;br /&gt;   echo 'Could not connect to mysql';&lt;br /&gt;   exit;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (!mysql_select_db('yub', $link)) {&lt;br /&gt;   echo 'Could not select database';&lt;br /&gt;   exit;&lt;br /&gt;}&lt;br /&gt;$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");&lt;br /&gt;$sendto2 = mysql_result($sendto1, 0);&lt;br /&gt;&lt;br /&gt;if (!$sendto2) {&lt;br /&gt;   echo "The alias does not exist! Use the command shortcut to create one.";&lt;br /&gt;exit;&lt;br /&gt;}&lt;br /&gt;echo "$sendto2";&lt;br /&gt;?&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 07 Nov 2005 19:32:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/872</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
  </channel>
</rss>
