NiftyHost Forums (Archive)
The Snippets Thread - Printable Version

+- NiftyHost Forums (Archive) (https://niftyhost.chary.us/support)
+-- Forum: Technology (https://niftyhost.chary.us/support/forum-7.html)
+--- Forum: Coding (https://niftyhost.chary.us/support/forum-12.html)
+--- Thread: The Snippets Thread (/thread-1339.html)



The Snippets Thread - MyDigitalpoint - 08-15-2011

Based on my own experience, snippets are a great way to learn PHP and sometimes a useful time-saver even if you know it because you can refer to these chunks of code for a quick integration into your coding.

Let's post small snippets that can be useful to other members whether to learn or add extra functionality to a website or blog, in example, WordPress using the PHP executable plugin.

The following snippet it's a directory listing so people can browse a directory on your site and even download files when you have setup a restriction via htaccess that does not allow directory listing:
[/quote]
Code:
<?php
$sd = '.';
$dir = opendir($sd);
echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>Directory Name</b></td><td><b>Dir Type</b></td><td><b>Last Modified</b></td></td>';
while($item = readdir($dir)){
if( (is_dir($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$lm.'</td></tr>';
}
}
echo '</table><hr />';
rewinddir($dir);

echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>File Name</b></td><td><b>File Type</b></td><td><b>Size</b></td><td><b>Last Mod.</b></td></tr>';
while($item = readdir($dir)){
if( (is_file($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
$fs = filesize($item);
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$fs.' bytes</td><td>'.$lm.'</td></tr>';
}
}
echo '</table>';
closedir($dir);
?>



RE: The Snippets Thread - HiddenKnowledge - 10-02-2011

This snippet is supposed to connect you to a mysql server and database using the basic mysql functions.
PHP Code:
<?php 
$config
['mysqluser'] = "USERNAME";
$config['mysqlpass'] = "PASSWORD";
$config['mysqldatabase'] = "DATABASE";
$config['mysqlhost'] = "HOST";

mysql_connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass']) or die(mysql_error());
mysql_select_db($config['mysqldatabase']) or die(mysql_error()); 



RE: The Snippets Thread - MyDigitalpoint - 10-20-2011

This snippet generates random hexadecimal number to get HTML colors

PHP Code:
<?php 
function get_random_color() 

    for (
$i 0$i<6$i++) 
    { 
        
$color .=  dechex(rand(0,15)); 
    } 
    return 
"$color"

?>

This can be useful to randomize the background color of a website page or a section, such as the header or navigation bars this way:


PHP Code:
<div style="background:#<?php echo $color; ?>;">

or <
body background="#<?php echo $color; ?>;"



RE: The Snippets Thread - Zach - 10-20-2011

Here's one of the ones I find extremely useful when parsing user input. It's a regex function that takes all non-numeric characters out of a string. [source]
PHP Code:
preg_replace('/\D/'''$string
Of course, you can replace the D with other regex strings, like [a-zA-Z] to remove all non alphabetic characters, or [a-zA-Z0-9] to remove all alphanumeric characters.


RE: The Snippets Thread - Arthur - 10-27-2011

When I saw this thread, I thought "Oh! I'll post my hex color code generator!", but obviously that would be pointless, as there already is one. So I'll post my snippet to delete all files in a directory (useful if you want to have a temp folder and it gets full of non-deleted useless files.)
PHP Code:
function empty_folder($folder){
    
$files glob("$folder/*");
    for (
$i 0$i count($files); $i++){
        
unlink($files[$i]);
    }


Function to highlight a string where $str is the string to be highlighted, $bg is whether it's the background or the text that's highlighted, and $color is what color to highlight it:
PHP Code:
function highlight($str$bg=true$color=false){
    
$type "";
    if (!
$color){
        
$color "#8B8B00";
    }
    if (
$bg){
        
$type "background-";
    }
    return 
"<span style='".$type."color:$color;'>".$str."</span>";




RE: The Snippets Thread - Zach - 10-27-2011

@Arthur: You might have some trouble with your first function. empty() is a pre-defined function in PHP.


RE: The Snippets Thread - Arthur - 10-27-2011

Oops. I posted a modified version of the function. The version I actually use is called delete_temp(). So I'll change the function name in my previous post.
Thanks for noticing!


RE: The Snippets Thread - Serial Thrilla - 01-18-2012

Some *nix / Linux batch script for HLDS Valve gaming servers.


Half-Life Dedicated Server (HLDS):
Code:
#!/bin/bash

# Starting
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
while true; do
./hlds_i686 -console -game x -ip x -port x +maxplayers x +map x -pingboost 3
done

Replace the "x" with your value and add other parameters if need.
_______________________________________________________

Source Dedicated Server (SRCDS):
Code:
#!/bin/bash

# Starting
while true; do
./srcds_run -console -game x -ip x -port x +maxplayers x +map x
done

Replace the "x" with your value and add other parameters if need.
_______________________________________________________

Create a empty file and paste this code, modify it, so it looks how you need it and save the file into the root folder of your server (where srcds_run or hlds_i686 is).

Source: http://forum.singularity.us.to/thread-96.html (Yes, I'm the person called Technician)


RE: The Snippets Thread - Arthur - 04-12-2012

PHP function to add a JavaScript alert ($id allows you to give it an identifier so you know which it is [perhaps a later version will allow auto-numbering]):
PHP Code:
function alert_flag($id=false){
    if (
$id){
        
$flag ' Flag: '.$id;
    }
    echo 
'<script type="text/javascript">alert("Is this active?'.$flag.'");</script>';