Wednesday, July 31, 2013

Email Validation on PHP

I have made email validation before, but have forgotten where I placed that code.  I had to look for the RegEx (Regular Expression) around the net, there are some that work at a certain point, but does not if you had something like .com.ph or .com.us or .com.au.

There is also a question about the why the hyphen in his code does not work:
http://stackoverflow.com/questions/7939147/preg-match-email-validation-adding-hyphen-to-domain-spot

There is also this one that works, well almost (upper case letters not allowed on this one):
http://www.developphp.com/view_lesson.php?v=224

Explanations on how to add a hyphen to the Regular Expression:
http://stackoverflow.com/questions/9589074/regex-should-hyphens-be-escaped
http://stackoverflow.com/questions/6744587/simple-regex-match-any-string-with-at-least-one-hyphen


So I tweak  the code a bit to have the perfect email validation code I have seen so far, and here it is:

$email = "test@test-tester.com.ph" //<-- Works

$Syntax='/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/';

if (preg_match($Syntax, $email)) {
     echo $email . \" is a valid email. We can accept it.\";
} else { 
     echo $email . \" is an invalid email. Please try again.\";

Let me know if there is an improvement for this code.

Also a bit of caveat on the preg_match() function:
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred

as mentioned in PHP documentation: 

Tuesday, July 16, 2013

MySQL WorkBench problem cannot create user

'Unhandled exception: Error creating account' error message when creating an account in mySQL Workbench.

I'm using workbench 5.2.31 CE.

This problem have popped up, when I'm trying to create a user in mySQL.  

There are two steps to work around this:
  1. edit the sql-mode configuration.  It can be found under Advance tab, Edit the text of sql-mode.  Remove the part "NO_AUTO_CREATE_USER," or cut it.  Then paste it back after you have created your user.
  2. Run the grant all previleges script as instructed below.

The solution is very simple.  You just have to run one of this statement in the Workbench Query window:

GRANT ALL PRIVILEGES ON *.* TO changetousername@localhost;
GRANT ALL PRIVILEGES ON *.* TO 'changetousername'@'%';

This will create an account without password.  So better set the password and change the previleges.

I don't know if this is the best work around or the worst.  But if you are desperate to add a user account.  This is the way that worked for me.

Here is part of the source of the solution:
http://stackoverflow.com/questions/15673640/unhandled-exception-error-creating-account

Tuesday, July 9, 2013

Cookie Setting troubles in PHP

I was trying to determine why the value for a cookie is not reflecting on my page.

Here are some of other developers say you should now in implementing cookies:

  1. It has to be on top before any code is generated by the page
  2. Cookie will not reflect on the same page when you set it.  It will reflect on the next loading of the page.
2nd one is true.  The first one also seems to be true, since no value is placed when I placed the code in the middle and after html codes have been rendered.

Here are the links to those recommendations:


There is a problem with the cookie when the mobile browser is never closed.  I had my browser open in the background.  Even when I set the cookie to expire in 30 mins.  It does not happened, since the browser still reads the value even beyond expire date.