Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

shantanu oak http://oksoft.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS 

Querystring variables

// Add Querystring Variable
// A PHP function that will add the querystring variable $key with a 
// value $value to $url. If $key is already specified within $url, 
// it will replace it.

function add_querystring_var($url, $key, $value) {
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
if (strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
}

// Remove Querystring Variable
// A PHP function that will remove the variable $key and its value 
// from the given $url.

function remove_querystring_var($url, $key) {
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
return ($url);
}
 

Unicode words from online dictionary

// list words from unicode dictionary
// you need to add this line in the head section
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

for ( $i = 1; $i <= 45; $i++) {
$url="http://dsal.uchicago.edu/cgi-bin/romadict.pl?page=$i&table=molesworth&display=utf8";
$text=file_get_contents($url);
$myarray = preg_match_all('#<font size="\+1">(.*?)</font>#i', $text, $matches);
echo implode(' ',$matches[1]); 
}

Password Protection

// Bare Bones demo of password protection

<?php
$password = "abc123";
if ($_POST[doddle] == $password) {
        $tell = 1;
} else {
        $tell = 2;
}
if (! is_string($_POST[doddle])) $tell = 0;
?>
<html>
<head><title>A Page that is password protected</title></head>
<body>
<?php if ($tell == 1) { ?>
 
<h1>The page with all the secrets revealed </h1>
 
This is the information that has been revealed to you because you
got the password right!<br /><br />Although this is a very simple
demo it shows what you can do with just a few lines of PHP.
 
<?php } else { ?>
 
<h1>A Header page</h1>
 
There is a page hidden under a password at this URL. For this demo only
I will tell you that the password is "abc123" so you can try it out!<br />
<?php if ($tell == 2) print ("<br />YOU GOT THE PASSWORD WRONG<br />"); ?>
<br />
<form method=post>
Please enter password <input type=password name=doddle>
<input type=submit value=go>
</form>
 
<?php } ?>
 
<hr />
<a href=http://www.wellho.net>Well House Consultants</a>, 2007
</body>
</html>

tinyurl explode

// check where tinyurl.com is headed to

    < ?php

    // tinyurl.php?c=

    $num = $_GET['c'];

    if($fp = fsockopen ("tinyurl.com", 80, $errno, $errstr, 30))
    {
    if ($fp) {
    fputs ($fp, "HEAD /$num HTTP/1.0\r\nHost: tinyurl.com\r\n\r\n");
    while (!feof($fp)) {$headers .= fgets ($fp,128);}
    fclose ($fp);
    }
    $arr1=explode("Location:",$headers);
    $arr=explode("\n",trim($arr1[1]));
    echo trim($arr[0]);
    }
    ?>

Display all files in a directory

<?
/**
* Change the path to your folder.
* This must be the full path from the root of your
* web space. If you're not sure what it is, ask your host.
*
* Name this file index.php and place in the directory.
*/
    // Define the full path to your folder from root
    $path = "/home/content/s/h/a/shaileshr21/html/download";
 
    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");
 
    // Loop through the files
    while ($file = readdir($dir_handle)) {
 
    if($file == "." || $file == ".." || $file == "index.php" )
 
        continue;
        echo "<a href=\"$file\">$file</a><br />";
 
    }
    // Close
    closedir($dir_handle);
?> 

Alternating Row Colors

// tutorial from http://www.webreference.com/programming/php_color/

<?php  
// TEST1: FOR - Control  
for ($k = 0; $k < 10000000; $k++) {  
$class = (1) ? "bg1" : "bg2";  
}  

// TEST2: FOR - Remainder division  
for ($k = 0; $k < 10000000; $k++) {  
$class = ($k % 2 == 1) ?"bg1" : "bg2";  
}  

// TEST3: FOR - Negation  
$b = true;  
for ($k = 0; $k < 10000000; $k++) {  
$class = ($b) ? "bg1" : "bg2";  
$b = !$b;  
}  

// TEST4: WHILE - Control  
$k = 0;  
while ($k < 10000000) {  
$class = (1) ? "bg1" : "bg2";  
$k++;  
}  

// TEST5: WHILE - Remainder division  
$k = 0;  
while ($k < 10000000) {  
$class = ($k % 2 == 1) ?"bg1" : "bg2";  
$k++;  
}  

// TEST6: WHILE - Negation  
$k = 0;  
$b = true;  
while ($k < 10000000) {  
$class = ($b) ? "bg1" : "bg2";  
$b = !$b;  
$k++;  
}  
?>  


Validate email and domain

// Vlidate email and domain name
<?php

function validate_email($email){

$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

if(eregi($exp,$email)){
	if(checkdnsrr(array_pop(explode("@",$email)),"MX")){
		print("$email is ok.<br>");
			}else{
			print("$email is ok. But domain is not.<br>");
			}
				}else{
				print("$email is not ok.<br>");
				}   
}

validate_email("shantanu.ok");
validate_email("shantanu.ok@gmail.com");
validate_email("shantanu.ok@fsjaldkfjlsfjsljflsfjsldk.com");

?>

FTP to get a file from within PHP

$conn_id = ftp_connect("www.yoursite.com");
$login_result = ftp_login($conn_id, "username", "password");

if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}

// get the file
$local = fopen("local.txt","w");
$result = ftp_fget($conn_id, $local,"httpdocs/trlog.txt", FTP_BINARY);

// check upload status
if (!$result) {
echo "FTP download has failed!";
} else {
echo "Downloaded ";
}

// close the FTP stream
ftp_close($conn_id);

Yubnub "echoshortcut" to display text stored for alias

<?php

if (!$link = mysql_connect('', '', '')) {
echo 'Could not connect to mysql';
exit;
}

if (!mysql_select_db('yub', $link)) {
echo 'Could not select database';
exit;
}
$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");
$sendto2 = mysql_result($sendto1, 0);

if (!$sendto2) {
echo "The alias does not exist! Use the command shortcut to create one.";
exit;
}
echo "$sendto2";
?>

Yubnub "checkshortcut" command to check alias

<?php

if (!$link = mysql_connect('', '', '')) {
   echo 'Could not connect to mysql';
   exit;
}

if (!mysql_select_db('yub', $link)) {
   echo 'Could not select database';
   exit;
}
$sendto1 = mysql_query("select email from yubmail where mail_alias = '$path'");
$sendto2 = mysql_result($sendto1, 0);

if (!$sendto2) {
   echo "The alias does not exist! You can try to create one or send the message to mailinator ailas.";
exit;
}

echo "The alias $path does exist!";
?>
« Newer Snippets
Older Snippets »
Showing 1-10 of 13 total  RSS