|
|
PHP Code Snippets
|
|
| Quote: |
//
// check for errors
//
// error messages
$thickzero = 'Thickness cannot be "0".';
$errorcalc = '';
foreach($HTTP_POST_VARS as $key => $val) {
// this will search the array for any key containing the string "thickness"
// and then check the values for it. i.e it works for "thickness01" as well as "thickness6".
if (stristr($key, 'thickness')) {
if ($val == "0") {
if (stristr($errorcalc, $thickzero)) {
} else {
$errorcalc .= $thickzero . "<br>";
}
}
}
}
|
 
|
| Calling an array entry dynamically
|
|
You can use this to call an array entry dynamically:
| Quote: |
$HTTP_POST_VARS['thickness' . $i]
|
While using a for statement with an $i variable, this will find an entry in the HTTP_POST_VARS array called "thickness0", "thickness1", "thickness2", "thickness3" etc....
 
|
|
|
| Code: |
// this will determine if your using Firefox
$firefox = stristr($_SERVER[HTTP_USER_AGENT], 'Firefox');
|
follow with this:
| Code: |
if (!$firefox) {
} else {
Do something (change this to what you want done if it is firefox)
}
|
This can also be used for Internet explorer by using "MSIE" or Opera by using "Opera"
 
|
| PHPBBFetchall logout info
|
|
Try this:
| Code: |
<a href="<?php echo $phpbb_root_path; ?>login.php?logout=true&sid=<?php echo $userdata['session_id'];?>&redirect=..<?php echo $_SERVER['PHP_SELF'];?>">Log Off</a>
|
Or for a button use:
| Code: |
<form action="<?php echo $website_url; ?><?php echo $phpbb_root_path; ?>login.php" method="post" target="_top">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="sid" value="<?php echo $userdata['session_id']; ?>" />
<input type="hidden" name="redirect" value="..<?php echo $_SERVER['PHP_SELF']; ?>" />
<input type="hidden" name="outside" value="1" />
<INPUT TYPE="submit" CLASS="button" VALUE="Log Off">
</FORM>
|
 
|
|
|
Tried to figure out the simplest way of getting every post, get, cookie var available while register_globals is set to off, and came to this conclusion :
| Code: |
foreach($_GET as $key => $value) {
$$key = $value;
}
|
will turn every variable in the GET array to a common variable with the same name.
e.g. if you request http://myserver/myscript.php?var1=111&var2=222, adding the function in myscript.php will create 2 vars :
$var1=111;
$var2=222;
This works with every pre-defined array, such as $_POST, $_COOKIE, and the like.
The same can be done with post data.
| Quote: |
//
// this code converts variables from the post data to php variables
//
foreach ($HTTP_POST_VARS as $key => $value) {
$$key = $value;
}
|
 
|
|
|
| Code: |
<?php
// fill an array with all files from the current directory
$fname = "$newpath";
$handle = opendir($fname) or die("couldn't open directory");
while (!(($file = readdir($handle)) === false ) ) {
if (is_file("$fname/$file")) {
if($file == "index.php")
continue;
$files[] = $file;
}
}
// Close
closedir($handle);
?>
|
Where $newpath is the directory of files you want in the array.
 
|
|
|
Fill an array with a list of directories:
| Code: |
<?php
// Open the folder
$dir_handle = opendir($newpath) or die("Unable to open $newpath");
// Loop through the files
while (!(($array = readdir($dir_handle)) === false ) ) {
if (is_dir("$newpath/$array")) {
if($array == "." || $array == "..")
continue;
$array2[] = $array;
}
}
// Close
closedir($dir_handle);
?>
|
Where $newpath is the path to the directory you want to open.
 
|
| Filesize translate function
|
|
| Code: |
<?php
//useful functions
//Translate file size to nearest denomination. example MB KB...
function size_translate($filesize)
{
$array = array(
'YB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'ZB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'EB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'PB' => 1024 * 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
'MB' => 1024 * 1024,
'KB' => 1024,
);
if($filesize <= 1024)
{
$filesize = $filesize . ' Bytes';
}
foreach($array AS $name => $size)
{
if($filesize > $size || $filesize == $size)
{
$filesize = round((round($filesize / $size * 100) / 100), 2) . ' ' . $name;
}
}
return $filesize;
}
?>
|
Usage:
| Code: |
$fs = filesize("$newpath$value");
$fsize = size_translate($fs);
|
Where is the path and file name of the file you want info for.
 
|
| View array and variable info
|
|
Use this to view Array and Variable info.
| Code: |
<?php
//use this line to view contents of a variable or array by removing the comments(example: $newpath)
//some superglobals: $GLOBALS, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES, $_ENV, $_REQUEST, $_SESSION
echo ('<pre>'); print_r($newpath); die();
?>
|
 
|
| Current Directory Function
|
|
Use this to get the current directory. This will not return anything if you run it in the root directory, it must be run at least one level below.
| Code: |
<?php
//functions get current directory
function current_dir()
{
$path = dirname($_SERVER[PHP_SELF]);
$position = strrpos($path,'/') + 1;
print substr($path,$position);
}
echo current_dir();
?>
|
 
|
|
|