code « Kevin Pajak – Interactive Web Developer/Designer – BLOG

Posts Tagged ‘code’

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…)

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…)

Clearing contents of $_POST array values (to avoid re-submitting form data upon page refresh)

Tuesday, February 2nd, 2010

After trying SEVERAL different possible methods & suggestions, I finally was able to achieve this by pointing my form’s PHP processing page to another page, rather than the same page itself (I really wanted to keep everything – form as well as form-handling all on the same page, but I found that I had to do this in order to prevent the user from re-submitting form data [through the contents of the $_POST array] that does not clear upon a user’s hitting the refresh button on the browser). And following that, I had my form’s processing page script then re-direct back to the original page where the form was located:

// redirect to the same page without the POST data
header(“Location: original-page-with-form.php”);
die;

Here are some of the pages I consulted in my research:

http://www.thefutureoftheweb.com/blog/get-redirect-after-post
http://www.issociate.de/board/post/171234/Clear_POST_variables_on_page_refresh.html
http://www.mail-archive.com/php-list@yahoogroups.com/msg04635.html
http://qaix.com/php-web-programming/437-896-clear-post-variables-read.shtml

*NOTE: Of course, keep in mind that any header(“Location: PAGE”); PHP code must be included BEFORE any contents are outputted on the page, otherwise you will get the lovely “Headers already sent” PHP error.

Hope this can help anyone else out there who may be struggling with the same issue :)