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 23 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);
}
 

delete empty directories

// commands to recursively delete empty directories below the current one
// (use at your own risk, as the slightest mistake WILL destroy 
// all of your data - Linux) 
find -depth -type d -empty -exec rmdir {} \;

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]); 
}

extract table names from sql log file

grep "from " /var/log/mysql/mysqld.log | awk -Ffrom '{print $2}' | awk '{print $1}' | cat > /home/shantanu/testing.txt

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]);
    }
    ?>

mysql dump to another server

// Using UNIX pipe concept one can dump database to another server securely using ssh protocol.
// All you need remote execution rights for the ‘dd’ command, over SSH.

mysqldump -u USERnAME -p'PASSWORD' YOUR-DATABASE-NAME | ssh user@remote.server.com "dd of=/mysql/$(date +'%d-%m-%y')"

email addresses using egrep

// email addresses

egrep "\w+([._-]\w)*@\w+([._-]\w)*\.\w{2,4}"

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++;  
}  
?>  


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