By BoLOBOSE payday loan

Redundant Data Class in PHP {Theory}

As a server side programmer you are going to be spending a lot of time working with data that is pulled from the database and in order to make sure that you do not have any loose ends it is a good idea to handle this situation with a class. This article is not going to cover how to code a data class nor is it going to help you create a specific data class. This article is here to show you how to make your data redundant and error proof.

Pulling all of your information

When pulling the information off the get go you are going to want to do one thing and one thing only: query all of the data that you can use in the class. The query is going to take place inside of a function and will most likely be used every time that you are in need of the data in the database. Once the query to obtain this information has taken place then you are going to either have results or no results and the easy way to check is simply by checking the number of rows returned.

Say we were trying to access user information for a user page. It would be a good idea to query and make sure that all of the information is available. In this query we are most likely going to be joining up to multiple tables because having all of the user information stored in one table would create a fair amount of overhead and would increase the time it took to go through the table. For this reason we break things apart.

An Example Of Database Tables

A well structured database table is going to be pulling from an auto incrementing ID. This auto incrementing id is going to be found on every table but since we can not just assume that if we insert a record into tbl_user and tbl_user_profile at the same time we are going to get ID’s that match properly. Because this is a flawed way of looking at the table set up we are going to use the auto incremental ID from the tbl_user and plug this into a new column on the other user tables. This way we can simply join the tbl_user.user_id to tbl_user_profile.user_nid. Now that we have a relationship in the database that will work and is pretty redundant in theory we are still going to have to deal with the situation of a table not being created properly upon user registration and thus we are lead to…

Enforcing Existing Tables

Remember back up at the top when I was talking about the query all function either working or not working? Well this function upon returning zero rows will tell us one of two things. (Assuming the SQL was written properly.) The first thing that it will tell us is that this user does not exist, and the second thing is that the user is missing a table. This could mean that something was deleted, or maybe the table was recovered in backup at a later date and there was a record missing. Either way we know that by checking the user table for the users existence we are able to confirm that we are dealing with missing data and this is where the checking functions come into place.

In a well written data handling class there will be functions that exist to pull results from each of the individual tables. These functions are excellent for checking that a user does exist in the following table and are a great way to quickly and easily pull sections of content based on the user which are handy when loading content on an interval via AJAX. But now we are getting a little off topic. Creating a function that can pull information and return a number as a status and creating another function that will insert defaults into the table is an excellent way to make sure that you do not lose data and that all of the data is being pulled properly.

Recap

When using the class you are most likely just going to need to get the information which means that you will be using a function that behaves like the queryAll that I was talking about. If the user does not exist then you can simply return false.

The next step is to create functions that will check individual tables based on that original tbl_user.user_id (or whatever yours is) starting with the initial tbl_user which the rest of the tables are based on. This will tell you if it was created in the first place or not.

If we have gotten far enough to know that the user does exist and the user simply doesn’t have a record in one of the tables then we can query each of the tables and find out where the record is missing. Now that we know the table we can simply insert blank data into the table and maybe send a notification to the user that they may need to update a certain part of their profile.

Knowing when we have an error in table creation

Through this data class it is just a matter of adding in an error log message to let the administrators tell if the application has a bug in it but this shouldn’t be a problem if you are handling proper inserts and updates through SQL and checking for an affected row upon creation.

cURL 403 Error Returning

The other day at work we ran into an issue where the server would return a 403 error page when retrieving page information from a cURL call. After searching around the web for a while thinking that we had a server permission issue on our hands it ended up just being a PHP problem.

In order to make a cURL request from your own server you must first make sure that the session has been destroyed prior to and cURL commands. This is because your server cannot have two pages that can access sessions up at the same time and the primary file that you are working from is going to lock the secondary file that you are trying to bring in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
session_start();
 
//authentication code.
 
//destroy session first.
session_destroy();
 
//cURL code.
$ch_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$response = curl_exec($ch)
curl_close($ch);
 
//now we can start the session again
session_start();
 
//do some other stuff...

My only guess as to why PHP does this is to protect itself from breaking sessions with multiple page access which is a pretty good security issue to have in place. Better error reporting would have been nice though.

Web Browsers for Web Developers

Introduction

This articles focus is fully based on what browsers are best for web development and industry standards say that we need to be developing to support all major browsers and IE 7+ however, this does not mean that you should be using some of theses browsers in your day to day activities on the web.

In my eyes there are only two main browsers that are acceptable as primary browsers, the first is Google Chrome and the second is Mozilla Firefox. Both of these browser have their pros and cons and thus I will be going through why I picked one over the other for primary use.

The Chrome

I’m just going to throw out there that I am a rather large fan of Chrome and thus it is my automatic choice but both browsers have merit. The reason that I pick Chrome over Firefox is because of the slick and easy inspect element feature that is very handy in checking for ajax calls and responses, along with JavaScript errors and the ability to use an incognito window. This kind of window does not hold cookie information or anything else after the window is closed. This means that you can throw up multiple incognito windows which can really step up your ability to debug server requests.

And Firefox

Firefox has an excellent tool set that can be installed and this tool set is known as firebug. It can tell you returned responses that are not visible to the user in something like Google Chrome. Firefox is also an excellent use for Flex 4 debugging which is why I do end up using it a fair amount when working on side projects.

The Comparison

There really isn’t that much to compare, both browsers have merit and I often switch between each of the two as a primary browser. Each browser has a lot of html 5 support but Chrome features things such as browser side storage. The only thing that you should take out of reading this rather short article is that it is time to switch away from Internet Explorer and probably Opera.

Firefox - good support for html5 and css3 along with great debugging features.

Chrome - excellent support for html5 and css3 and it has an excellent amount of pre-built in features for debugging.

Safari – Even though I am not a supporter of Apple this browser is great for supporting modern features but has a poor set of built in tools for debugging when it is right out of the box.

Opera – It has a decent support for the new features but is not in the same class as Firefox and Chrome.

Internet Explorer – Horribly supported and should be renamed “download Firefox or Chrome.”

PHP: Array Of Bad Words

When creating applications that are going to be used by hundreds of thousands; it is important to make sure that you have the proper facilities in place to handle curse words that are entered in by users. This can be done by checking an array of bad words.

The code is simply…

1
2
3
4
5
<?php
//foul language array
$this->badWords = array('word1', 'word2','word3');
//Now you just need to go through your string and make comparisons.
?>

Rather then posting the code directly onto the blog I would rather have a site that is safe for all readers and not be indexed with foul language and racial slurs and thus is why I am offering the array via a text file within a compressed zip.

JavaScript: setTimeout Firing Immediately

Today at work I needed to do some Ajax calls to update some information in the database and pull the most recent results for the administration side of the project. I was stumped for about 10 minutes while trying to figure out why my button would not change back to its original state after displaying “updated.” It turns out that when you call a function using ‘setTimeout’ and you do not wrap the function in quotation marks, the function will be triggered immediately.

1
2
3
4
5
//function is triggered immediately.
setTimeout(updateRecord(), 500);
 
//function is triggered after the set time.
setTimeout("updateRecord()", 500);

This immediate triggering makes sense when you think about how the triggering within the parameters operates but it got me all the same.

.htaccess Handling Bad Server Requests

The plain old and common 404 error can get a little old and for that reason I am going to fill you in on how to handle bad status reports that you can easily handle. In order to follow along you are going to need access to your .htaccess file along with a normal way to handle URLs without special mapping.

The Code

In order to make this work successfully we are first going to need to open up the .htaccess file. If you do not currently have one but know that your host supports the functionality then you can just open up notepad and save the file as “.htaccess”. With the file in place we are going to need to add the following lines of code to make the server check for proper results.

1
2
#force error document handling.
ErrorDocument 404 /error/404.php

As you can see in the above code we have a comment followed by an error document code. We have set the status type that we are after to 404 and then pulled the page from the directory “error” and requested the file named 404.php.

When requesting a page like this you are not actually redirecting the user to the new page but rather you are bringing in the 404.php to be displayed. This allows the user to keep the attempted url in the window in case they need to make a change to the spelling.

Adding More

If you would like to handle things such as 401 requests as well you can do the same thing on the next line below. It will allow you to handle a different error with the same page or you can always change up the page to display something else. This allows for unique page error messages.

1
2
3
ErrorDocument 401 /error/404.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php

Why It Won’t Work With URL Mapping

When working with URL mapping in a way that allows you to build the application from the index file you shouldn’t have to deal with handling status’ at all. This is because you handle all of the fake directories and if a directory doesn’t match up to a predefined one then you can simply just throw it to a 404 page if you desire.