Skip to content

Instantly share code, notes, and snippets.

@psdtohtml5
psdtohtml5 / gist:5646810
Created May 24, 2013 22:03
PHP: Secure File Download
<?php
// PHP SECURE FILE DOWNLOAD
// Put the Files in a directory that is not publically accessible.. maybe outside document root or set correct permissions
if (!$_SESSION["authenticated"]) {
// If not authenticated then send back to the login page
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/login.php?err=true");
exit;
@psdtohtml5
psdtohtml5 / gist:5646814
Created May 24, 2013 22:04
PHP: PDO database connect
<?php
// Connect to database using PDO
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
@psdtohtml5
psdtohtml5 / gist:5646820
Created May 24, 2013 22:04
PHP: PDO Prepared Statement
<?php
// PDO Prepared statements
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
$stmt->execute(array('id' => $id));
@psdtohtml5
psdtohtml5 / gist:5734835
Created June 8, 2013 11:04
PHP: Pagination
<?php
try {
$company = $conn->prepare('SELECT `id`, `name`, `name_lang2`, `is_active`, `sort_order` FROM `companies`' );
$company -> execute();
$count = $company->rowCount();
}
@psdtohtml5
psdtohtml5 / gist:5743421
Created June 9, 2013 13:00
PHP: Prepare parameters for PDO Quries
<?php
/**
* This function loops through the $_POST global and returns parameters that can be used in
* a PDO statement directly. Note : For this function to work properly the
* PDO::ATTR_EMULATE_PREPARES should be set to "false"
* like so "$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false)".
* @param Array $exclude This is an array of keys in $_POST that you want the function to ignore
* @return Array The function returns an array that can be used as parameters for the PDO statement
*/
function get_params($exclude = array()) {
@psdtohtml5
psdtohtml5 / gist:5842736
Created June 22, 2013 21:46
jQuery: Checkbox Change Event
$(".checkbox").change(function() {
if(this.checked) {
//Do stuff
}
});
@psdtohtml5
psdtohtml5 / gist:5871540
Created June 26, 2013 20:50
SQL: Update column with random numbers
UPDATE users SET password = floor(rand() * 9999999999) WHERE role=4
@psdtohtml5
psdtohtml5 / gist:5908031
Created July 2, 2013 09:46
jQuery: Drag and scroll horizontal div
<script type="text/javascript">
$(document).ready(function() {
$('.dragger').mousedown(function (event) {
$(this)
.data('down', true)
.data('x', event.clientX)
.data('scrollLeft', this.scrollLeft)
.addClass("dragging");
return false;
@psdtohtml5
psdtohtml5 / gist:5974960
Created July 11, 2013 12:18
PHP : Dynamic Image Resize
<?php
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
@psdtohtml5
psdtohtml5 / gist:5991280
Created July 13, 2013 16:41
PHP : Recursively delete files and folders
<?php
//define('UPLOADFILES', dirname(dirname(__FILE__)) . '/sixpoints');
function recursiveDelete($path) {
// if (strpos($path, UPLOADFILES) === false) {
// exit('Trying to remove from wrong path to uploadfiles!');
// }
if (is_file($path)) {
return unlink($path);
} elseif ( is_dir($path) ){