Skip to content

Instantly share code, notes, and snippets.

@MWins
Last active January 25, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MWins/6f438c06a33f5bb78223 to your computer and use it in GitHub Desktop.
Save MWins/6f438c06a33f5bb78223 to your computer and use it in GitHub Desktop.
php-code-stuff-use.
//old php - inspect and verify if still applicable
Twitter Regular Expressions (might require updating urls)
$str = preg_replace('/(https{0,1}:\/\/[\w\-\.\/#?&=]*)/', '<a href="$1">$1</a>', $str);
$str = preg_replace('/@(\w+)/', '@<a href="http://twitter.com/$1" class="at">$1</a>', $str);
$str = preg_replace('/\s#(\w+)/', ' <a href="http://twitter.com/#!/search?q=%23$1" class="hashtag">#$1</a>', $str);
require_once(dirname(__FILE__) . '/config/database.php');
require_once dirname(__FILE__) . '/bar.php';
//PHP 5.3
require_once __DIR__ . '/bar.php';
echo uniqid();
$start_time = microtime(true);
register_shutdown_function('my_shutdown');
// do some stuff
// ...
function my_shutdown() {
global $start_time;
echo "execution took: ".
(microtime(true) - $start_time).
" seconds.";
}
foreach (range('A', 'Z') as $letter) {
echo $letter;
}
debug_backtrace()
[ config.php ]
return array(�hello� => �world�, �foo� => �bar�, �name� => �john doe�);
[ index.php ]
$config = require(�config.php�);
print_r($config);
it will output:
Array (
[hello] => world
[foo] => bar
[name] => john doe
)
/* CMS TUTORIAL XSS FIX */
public function write($p) {
if ( $p['title'] )
$title = htmlentities(mysql_real_escape_string($p['title']));
if ( $p['bodytext'])
$bodytext = htmlentities(mysql_real_escape_string($p['bodytext']));
function template($name, $args=NULL){
$er = error_reporting(E_ALL&~E_NOTICE&~E_WARNING);
extract($args, EXTR_REFS);
$return = include(TEMPLATE_DIR.'/'.$name.'.php');
error_reporting($er);
return $return;
}
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
Most of the JavaScript frameworks like jQuery, mootools send and additional HTTP_X_REQUESTED_WITH header when they make an AJAX request, so that you can detect AJAX request on server side.
if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
//If AJAX Request Then
}else{
//something else
}
ini_set("zlib.output_compression","On");
ini_set("zlib.output_comression_level",4);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment