<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File to track

<script type="text/javascript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>

<body onLoad="MM_goToURL('parent','pdfs/file_name_here.pdf');return document.MM_returnValue">

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-1234567-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>

php – The execution operator

Dec, 3 -- Categories: PHP

**** note the back ticks not single quote ******

executes the contents of the ticks as if you are at the terminal but as apache or whoever your web server is. this code basically

echo `whoami`; // well who is this script running as

echo `pwd`; // think whereami
echo `mkdir test`; // make a directory
echo `mkdir test/anothertest`; // make a directory in the directory created
echo nl2br(`ls`); // list what is in this directory

`touch log.txt`; // make a text file named log.txt

$date = date("F j, Y, g:i a");
`echo $date > log.txt`; // writes the $date to log.txt
`echo foo >> log.txt`; // writes foo to log.txt
echo nl2br(`cat log.txt`); // echoes what is in log.txt

echo nl2br(`tail /opt/local/apache2/logs/error_log`); // echoes what is in the error_log

PHP alpha numeric only strings

Oct, 17 -- Categories: PHP

$str = “@ % * alpha numeric 0 – 9 only _ – +@_#()()++”;
$str = preg_replace(‘/[^A-Za-z0-9]/’, ”, $str);

echo $str;
returns alphanumeric09only

PHP Short tags

Oct, 10 -- Categories: Apache, Linux, PHP

enable by setting the flag in .htaccess

php_flag short_open_tag on

Open php.ini ( /etc/php.ini or /usr/local/etc/php.ini), enter:
# vi php.ini

Set short_open_tag to On:
short_open_tag = On

Save and close the file. Restart webserver:
# service httpd restart
or
# /etc/init.d/httpd graceful

To replace spaces with underscores and then remove everything that isn’t alpha-numeric open up

wolf/plugins/file_manager.FileManagerController.php

and replace the existing upload function (approx line 259) with this.

public function upload() {
$data = $_POST['upload'];
$path = str_replace('..', '', $data['path']);
$overwrite = isset($data['overwrite']) ? true: false;

if (isset($_FILES)) {

// $file = upload_file($_FILES['upload_file']['name'], FILES_DIR.'/'.$path.'/', $_FILES['upload_file']['tmp_name'], $overwrite);
$clean_file_name = preg_replace('/ /', '_', $_FILES['upload_file']['name'] );
$clean_file_name = preg_replace('/[^A-Za-z0-9_.]/', '', $clean_file_name );

$file = upload_file( $clean_file_name, FILES_DIR.'/'.$path.'/', $_FILES['upload_file']['tmp_name'], $overwrite);

if ($file === false)
Flash::set('error', __('File has not been uploaded!'));
}
redirect(get_url('plugin/file_manager/browse/'.$path));
}

The Ternary Operator

Jul, 17 -- Categories: PHP

$variable = condition ? if true : if false;

as in:

$age = 24;
$drinkingAge = ($age > 21) ? “have a beer” : “have an iced tea”;

echo $drinkingAge; // returns have a beer

PHP – date menus

Jun, 5 -- Categories: PHP

<?php

$today = getdate();

$day = $today['mday'];
$month = $today['mon'];
$year = $today['year'];
$mtharr = array(“January”,”February”,”March”,”April”,
“May”,”June”,”July”,”August”,”September”,
“October”,”November”,”December”);

?>

<select name=”start_day”>

<?php

for ($i=1;$i<=31;$i++) {

echo “nt<option value=”$i”";

if ($i == $day) echo ” selected”;
echo “>$i</option>”;

}

?>
</select>

<select name=”start_month”>
<?php

for ($i=1;$i<=12;$i++) {

echo “nt<option value=”$i”";
if ($i == $month) echo ” selected”;
echo “>” . $mtharr[$i-1] . “</option>”;

}

?>

</select>

<select name=”start_year”>
<?php

for ($i=0;$i<=9;$i++) {

$tmp = $today['year'] + $i;
echo “nt<option value=”$tmp”";
if ($today['year'] == $tmp) echo ” selected”;
echo “>” . $tmp . “</option>”;

}

?>

</select>

neat_trim($your_text, 100);

function neat_trim( $string, $n, $close = '…' ) {

$len = strlen( $string );

if ($len > $n) {
preg_match('/(.{' . $n . '}.*?)b/', $string, $matches);
return rtrim($matches[1]) . $close;
} else {
return $string;
}

}

look in your /system/application/errors/ and find the error_404.php and put this (edited to match your site of course)

<?php
header(“HTTP/1.1 404 Not Found”);
header(“location:http://www.tldomain.com/index.php?/controller/sitemap/404″);
?>

So when the 404 page loads the 404 still gets logged by apache etc… but once that happens it gets rushed of to (in this case) the site map. The 404 variable tells the controller method in this case the site map to display a this page cannot be found message.

Codeigniter app log helper

Apr, 24 -- Categories: PHP

This is for debugging only. Ok, again…. this is for debugging only. If you use this REMOVE it once it goes live. At worst you forget to add the tag that clears the log file each time and it bloats horribly and your site gets progressively slower and slower, then your server will crash, you’ll get fired and end up homeless. But if you remove it when your done it’s pretty handy.

This goes in a helper file of your choosing and make sure you load it:

$this->load->helper('the_helper_where_you_put_this_code');

Then put this in the helper file you loaded

if ( ! function_exists('app_log')) {

function app_log($arg) {

// this is your log file, so I referenced it by viewing
// domain.com/applog.htm
$log = './applog.htm';

// to empty the file and start over the first call should begin with init_log
if( preg_match(“/^init_log/”, $arg) ) {

// writes the the file name and whatever the arg is at the start of the file
if (!write_file($log, ''.$arg, 'w') ) {

// or dies
die( 'Unable to write to log file');

}

} else {

// adds whatever the $arg is to the end of the file
if ( ! write_file($log, ''.$arg, 'a+')) {

// or dies
die( 'Unable to write to log file');

}

}

}

}

To use the helper:

app_log('init_log -> Whatever you want goes here');

I usually make the above call in the constructor, then after that in your methods whatever you want to track use:

app_log('blah blah blah');

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Here is roughly how I did that:

http://www.jamesborder.local/URLRequest.php (your url should be wherever your data is)

<?php

// create an array to put your junk in… then put it there
$variablesArray = array();
$variablesArray['var1'] = “foo”;
$variablesArray['var2'] = “bar”;

// let php build a nice string for you
$variablesArrayString = http_build_query($variablesArray);

echo $variablesArrayString;
// var1=foo&var2=bar

?>

-=-=-=-=-=-=-=-=-=-

Here is roughly what is in the .fla

// build your request…. your url/file will go here
var request:URLRequest = new URLRequest(“http://www.jamesborder.local/URLRequest.php”);
request.method = URLRequestMethod.GET;

// create
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, onCompleteHandler);
loader.load(request);

function onCompleteHandler(event:Event) {

trace(“completeHandler(“+event+”)”);
var var1 = event.target.data.var1;
var var2 = event.target.data.var2;

trace ('variable one: ' + var1);
trace ('variable two: ' + var2);

}