Skip to content

Instantly share code, notes, and snippets.

View NoMan2000's full-sized avatar

Michael Ryan Soileau NoMan2000

  • Surge Forward
  • Reno, Nevada
View GitHub Profile
@NoMan2000
NoMan2000 / A-Pen-by-Michael-Ryan-Soileau.markdown
Created October 18, 2013 16:04
A Pen by Michael Ryan Soileau.
<?php
/**
* @file
* Sets up debugging tools, if enabled.
* PHP Debug is very powerful, but you have to configure numerous files to get it to work.
*/
// Enable or disable debugging tools.
$tools = array(
'chromephp' => FALSE,
@NoMan2000
NoMan2000 / phpValidation
Created December 8, 2013 19:39
Basic PHP Validation functions
<?php
$errors = array();
function fieldname_as_text($fieldname) {
$fieldname = str_replace("_", " ", $fieldname);
$fieldname = ucfirst($fieldname);
return $fieldname;
}
@NoMan2000
NoMan2000 / phpfunctions
Created December 8, 2013 19:44
Basic PHP Functions
<?php
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
// This function will sanitize a string. Requires a connection.
function mysql_prep($string) {
@NoMan2000
NoMan2000 / PDO
Created December 10, 2013 04:57
PDO PHP
<?php
try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME .";port=" . DB_PORT,DB_USER,DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}
@NoMan2000
NoMan2000 / setRoot
Last active December 30, 2015 21:50
Set document root folders.
<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
<?php /* Another way of handling the same problem */
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/common/header.php";
include_once($path);
dirname(__FILE__);
@NoMan2000
NoMan2000 / filtInput
Last active December 30, 2015 21:49
Filter input, sanitize output
<?php
// if the value passed into the GET should be an int.
$intStorage = intval($_GET['someValue']);
// Using PDO
$db = new PDO();
$query = $db->prepare("SELECT someRow FROM table WHERE value = ?");
$query->bindParam(1,$bindedValue);
// The first argument is number of prepared statements, second is the value.
$query->execute();
// Boolean false if nothing is found.
@NoMan2000
NoMan2000 / distinctLoop
Created December 10, 2013 17:04
PHP loop over a DISTINCT Index Value with option fields
$sql_value = "SELECT DISTINCT siteID FROM users WHERE entityID = $someVariable GROUP BY siteID;";
$sql_res = mysqli_query($dropdown_connect, $sql_value) or die(mysqli_error($dropdown_connect));
while ($row_info = mysqli_fetch_array($sql_res)) {
echo "<option value='$row_info[0] . '>" . $row_info[0] . "<style='width:200px;'></option>";
} // End while loop
@NoMan2000
NoMan2000 / DocumentRootCheck
Last active December 30, 2015 22:49
Check for the PHP document root.
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
$headers_response = apache_response_headers();
@NoMan2000
NoMan2000 / Base
Created January 31, 2014 22:19
PHP Always have a base root folder
<?php
define("DS",DIRECTORY_SEPARATOR,true);
define('BASE_PATH',realpath(dirname(__FILE__)).DS,true);
$include_file = BASE_PATH . '.mysql.inc.php';
?>