Archive for the ‘php’ Category

PHP Datatype Validation

So, let’s say that you’re writing a function or some sort of script. and you need to sure you’re only getting a certain datatype as a value. Then you realize that PHP has no actual datatype enforcement like C or C++. You can set any variable to contain any kind of structure (strings, integers, arrays, etc) and you can later change it to any other structure later on the fly. This can be problematic depending on what you need to do.

With the function you’re supposedly writing, let’s say you wanted to make sure a argument you’re accepting via the PHP call is an integer. Well you could perform and is_int() function call by yourself, but thats not the point. The point of functions is to reduce tedious/repeated code. I made a class that has a function inside it that does all the testing for you depending on what  you, as a coder, is expecting. So passing “hi” into the validator when you specify you’re looking for an integer wouldn’t pass. It would pass if you stated you were looking for a string, though.

What’s also nifty is that i wrote it to accommodate for the event of expecting multiple kinds of datatypes. Like let’s say you want either an integer or a double (a decimal), you can simple combine them when you call the validator. If the value you give the validator is an integer or a double, it’ll spit back the datatype.

Okay, how do I have it check for datatypes? Every datatype that I could look for is assigned a number in base 2 (1, 2, 4, 8, 16, 32, etc) . I assign them base 2 values in hex, simple because I felt like it.

define(“SCRAPBOOK_DATATYPE_STR”, 0×01); // STRING
define(“SCRAPBOOK_DATATYPE_ARR”, 0×02); // ARRAY
define(“SCRAPBOOK_DATATYPE_INT”, 0×04); // INTEGER
define(“SCRAPBOOK_DATATYPE_DOUBLE”, 0×08); // DOUBLE

SCRAPBOOK_DATATYPE_STR is set to 1, _ARR is 2, etc etc. You get the point. When I call the function, the first argument/parameter is the value I need to test. The second is the (possible) datatype(s) I’m expecting. What you may or may not know about bit addition is that its very easy to figure out what bits are included. For example, the value of 6 is ONLY made up of 2 and 4, which in  this are the bits that are assigned to _ARR and _INT. Using this logic, we can pass multiple datatypes we might get (for example, we want a number which might be an integer or a decimal) and the function can check to see if the datatype of the value I’m checking for matches one of the datatypes I’m looking for.

Here is an example of looking for one datatype and then two datatypes:

//first instantiate the object
$validate = new Validator();

//would return SCRAPBOOK_DATATYPE_ARR
print $validate->validateDatatype(array(), SCRAPBOOK_DATATYPE_ARR).”\n”;

//would return SCRAPBOOK_DATATYPE_INT
print $validate->validateDatatype(5, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).”\n”;
//would return SCRAPBOOK_DATATYPE_DOUBLE
print $validate->validateDatatype(5.3, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).”\n”;

As you can see, the pipe (|) is used to add multiple of these bits together. The function would return the bit of the dataype found. this way if you’re expecting two datatypes, you can validate it and then see the exact type that was actually sent in.

Now how do i actually do the tests? well in the function I have something like this:

function validateDatatype($value = NULL, $expectedtypes = SCRAPBOOK_DATATYPE_STR)
{
//are we testing if this datatype exists?  ||   now we test!
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_STR) && (is_string($value)) )
return SCRAPBOOK_DATATYPE_STR;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_ARR) &&  (is_array($value)) )
return SCRAPBOOK_DATATYPE_ARR;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_INT) &&  (is_int($value)) )
return SCRAPBOOK_DATATYPE_INT;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_OBJECT) &&  (is_object($value)) )
return SCRAPBOOK_DATATYPE_OBJECT;

Essentially, i’m testing to see if we’re asking for each datatype, if we are asking for that datatype, i perform a function call to see if it actually is. If it’s not it keeps going until it returns something. if  it gets to the end of the function, that means it didn’t validate, so false is returned. The (xy) test basically checks to see if bit y exists within value x. So again,  for example, lets say you have a number 13. The only way to get a number 13 through this method is  with numbers 8, 4 and 1 (8+4+1 = 13). So if you did (13 & 4) it’d return true.  So on and so forth.

Ok, now that I gave you little snippets, check out the entire script so you can see how it all blends together.

Leap Seconds

So, I noticed when my employer moved to a new web-server running (at the time) the most up-to-date version of mySQL and PHP there were some discrepancies with dates.

Then I realized something. For some reason when certain functions of the website posted things to the database that involved time, it was off.  When the database entries did not have hours,minutes,seconds and just the date, it’d always push the date to be the previous day. This was strange and very annoying. You would post a news entry, and it’d be displayed as if it were posted the day previous. If you posted a sports schedule, the game listing would be off a day (this could cause definite issues).

After investigating this further, it turned out that database entries with hours,minutes,seconds did display the time as 23 seconds behind. Always 23 seconds. Always. This makes sense, because on date fields that don’t have the hourly time written, it’s internally notated as midnight (00:00:00).  So, if you take 23 seconds away from midnight, it’s the previous day. What could cause this? PHP (the programming language that’s calling the data from the database) and mySQL is running off the same machine with the same clock, so it’s not like it’s desync’d.

After even further investigation, I traced down the issue. Apparently the international time committee decided to implement leap seconds in the UTC time standard. These leap seconds are implemented to compensate for the gradual slowing of the earth’s rotation due to the tidal effect. mySQL accounts for the leap seconds. PHP doesn’t. Or maybe vice versa. I don’t know. I would think if leap seconds are added, it’d be 23 seconds ahead. Perhaps I’m just thinking backwards about this.

Apparently these people don’t think of the consequences of their decisions in regards to hardware and software where time precision is required. Especially when midnight is used for the baseline in setting times. Right now I’m going to investigate the possibility of changing the mySQL timezone in favor of a “time standard” that doesn’t reflect leap seconds. Perhaps GMT-5.  instead of UTC-6. Or whatever.

Regardless, this just shows why UTC should not be used when consistency is required.

PHP Powered IRC Services?

So. For awhile, I’ve been thinking of doing something. I probably really won’t have the time to do it, but I figure i might as well get it on “paper” and perhaps inspire someone to either join forces with me or do it themselves.

PHP Powered IRC Services.

Hmm. IRC stands for Internet Relay Chat. It’s basically a type of server that users can connect to. Then a user can join channels, which are in layman’s terms, a chat-room.

Well, in order to have ownership of nicknames/accounts and channels, you need to have a set of services to enforce it. For example ChanServ would be incharge of channel ownership and stuff like that while NickServ or AuthServ would be the services incharge of nickname or account ownership.  Read more about it on Wikipedia.

Okay, well. 90% of  the IRC Services software packages is written in some variant of C. C is a very good foundation for this kind of software deployment. But what about PHP? PHP, at least for me, is a lot easier to write in, mostly because of I’ve had many more years of experience in it. But think about it.. if you run it in PHP and use a mySQL database (or something similar) you can very, very, very easily integrate it into a website. Also, PHP doesn’t need to be compiled, just save changes and run it.

So, some caveats. Nonterminating PHP scripts tend to make the HTTP daemon apache freak out and consume a lot of CPU. However, you could use something like httplite, I suppose. Secondly, PHP isn’t the most secure environment and allows for a lot of ‘bad style’ of programming to be done without caring too much.

Also, I’d like to address PHP’s socket extension. In order for this whole thing to work, the PHP script would have to connect via a socket connection to the main server hub in the IRC network and it would have to act like another server. This would require the script to talk to the hub in a SERVER-to-SERVER protocol, rather than a USER-TO-SERVER protocol like regular IRC clients do. This is a protocol that I am not used to, so I’d have to read up on it. Also, PHP socket extension is still in the experimental stages of development.

I know every C programmer and IRC services package developer that is reading this is probably freaking out and pulling their hair out at the mere idea of a PHP powered package, cursing at me in languages they didn’t even know they knew. Well, it’s time to do something new.. if everybody did everything the same way, the world would be a boring place.

And it’s also just to see if it can be done. What’s wrong with experimentation?