Web Dev/Design « Kevin Pajak – Interactive Web Developer/Designer – BLOG

Archive for the ‘Web Dev/Design’ Category

How to delete entire folders/sub-folders (with all files contained within) in PHP

Wednesday, March 31st, 2010

Thanks to PeejAvery @CodeGuru.com for turning me on to this one (with the original code here).

Here’s the code: (more…)

WordPress plugins and widgets

Tuesday, February 16th, 2010

I will make some WordPress plugins and widgets, and already thinking of possibly using them to “plug in” some of the aspects of my app dev work in various capacities, if site requirements call for it or could use it ;) .

How to replace a backslash character in a string

Friday, February 12th, 2010

Ah, so often it’s the little things eh…

PROBLEM: I needed to find a way to “re-process” text a user enters for a live chat client I’m working on (in which I prevented MySQL database injections via the mysqli_real_escape_string function). The problem of course is that the text the user enters nicely protects you from a MySQL database injection, BUT the text then comes back full of backslash (\) characters (not too cool looking huh). So, to remove these, you can use the following code (here, I use the PHP str_replace function:

// Convert escaped characters back to regular text
$string = str_replace("\\", "", $string);

Thanks to Geoff Muldoon for this one!:
How to replace a backslash character in a string?

mysqli_real_escape requires TWO parameters!

Thursday, February 11th, 2010

Props to this really useful post on PHPFreaks.com:
http://www.phpfreaks.com/forums/index.php?topic=219045.0

I was having some issues with a mysqli_real_escape_string function that I had converted from mysql to mysqli I believe that the mere mysql version of this tag can function with just a $value, but the mysqli version requires TWO PARAMETERS: both the $connection variable, and the $string (whatever variable your particular function uses. The one I have here goes through all posts in a foreach statement and escapes the values of all posts using an array (to prevent MySQL injections).

Ex:
//This stops SQL Injection in POST vars
foreach ($_POST as $key => $value) {
$_POST[$key] = mysqli_real_escape_string($connection, $value);
}

$connection = whichever variable you would normally use to reference connection to your MySQL database(s).

THIS POST solved my problem! (Thanks a lot Bendude14!)

How to log out a user who closes the browser window

Wednesday, February 10th, 2010

I would like to thank Brazilian software developer Daniel Melo for posting this code which helped me to solve a problem which was very pesky indeed, and in need of a solution. The problem occurred during a user’s logged-in session for a chat application I was building. This was the issue: the app is basically a live-chat client, which also shows all current users online, by name. If a logged-in user clicked the “Logout” link, all was good, no problem, his/her name was removed from the list of currently logged-on users. However, if the person just suddenly decided to end the chat session by just closing the browser and hitting ‘X’ (as I myself might do!), the user was still showing as currently online, and NOT logged out (obviously this is an annoying issue). (more…)