BetterPHP PHP Library

Written something you are proud of, post it here.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: BetterPHP PHP Library

Post by bowersbros »

For password check (mainly register etc) is it worth having it so that there are certain 'security' requirements, that the coder of the site sets in the user class? for example, they can set 1 if they need it to be alphanum, or 2 if they need it multicase etc? or should i just let it be anything goes?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

let it be anything, and we can add some functions to the validate class to check for things like that. Then the developer can just use them if he wants.
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

Added two functions to the validation file for email addresses and urls. Two things which can be a pain to do well ;)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: BetterPHP PHP Library

Post by bowersbros »

Um, how do we use these to check if they are valid, if not say throw an error?

Im currently doing
$email = $validate->email($email);
if($email === 0){ //echo }
I feel that is wrong though.

is that what i should be doing or?
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

the validation class should be used staticly
if (validate::email($email) === false){
    // invalid email.
}
Image
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: BetterPHP PHP Library

Post by conradk »

Hello,

I'm gonna try to make some kind of a form builder class. It would allow to easily create HTML forms and automatically include the appropriate validation.

I'm kind of a noob though, so it might take a while to end up with a crappy result :lol:

Regards,
CK
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: BetterPHP PHP Library

Post by bowersbros »

That could be very useful actually :)

Just give it your best shot, and we'll help you fix problems, or improve it and such :)
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: BetterPHP PHP Library

Post by conradk »

bowersbros wrote:That could be very useful actually :)

Just give it your best shot, and we'll help you fix problems, or improve it and such :)
Yep, I'll give it a try. I'm tired of rebuilding form validation each time I make a form. There's gotta be a way to make everything easier :)

EDIT 1

I just found this, a PHP5 class library:
http://sourceforge.net/projects/pacl/

There are some pretty useful things in there :) Just have a look.

And this in case someone is interested. Some useful things in there too:
http://code.google.com/p/cmclasses/

EDIT 2
// checks to see if $haystack starts with $needle
function str_starts_with($needle, $haystack){
	$needle_len = strlen($needle);

	return ($needle_len <= strlen($haystack) && substr($haystack, 0, $needle_len) === $needle);
}

// checks to see if $haystack ends with $needle
function str_ends_with($needle, $haystack){
	$needle_len = strlen($needle);

	return ($needle_len <= strlen($haystack) && substr($haystack, -$needle_len) === $needle);
}

// checks to see if $haystack contains $needle
function str_contains($needle, $haystack){
	return (strpos($haystack, $needle) !== false);
}
Wouldn't it be easier to just use a Regex ? Would be just as easy, don't you think ? Like preg_match('#hello#',$string) would be the same as string_contains('hello',$string).
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

function str_starts_with($needle, $haystack){
    $needle = preg_quote($needle);
    return (preg_match("#^{$needle}#", $haystack) === 1);
}
Should work actually. At the time that didn't cross my mind. I would imagine that it would be slower, but will have to test.

Also, for these files it is not about doing something the easiest way, since when you use it, it is already done.
Image
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: BetterPHP PHP Library

Post by conradk »

jacek wrote: Should work actually. At the time that didn't cross my mind. I would imagine that it would be slower, but will have to test.
Hadn't thought about speed :/ And also, just afer I wrote that, I realised that I was building classes a little bit the same way, since it gives you the ability to change the code later without having to update applications, which is quite nice :)

Here are some classes I'm working on (gotta start the form class soon too :oops: ). Please tell me whether you think this is useful or not :) Feel free to add any of this to the BetterPHP library if you find it useful or to use this code anywhere. I don't mind :)
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

class Validation {
   const email_pattern = '#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#';
   const fr_phone_pattern = '#^0[1-68]([. -]?[0-9]{2}){4}$#';
   const url_pattern = '#^(http(|s)|ftp)://([0-9a-z.-_]+:[0-9a-z.-_]+@)?[0-9a-z.-]+\.[a-z]{2,4}(:[0-9]{2,6})?(/.+)?#i';
   const date_pattern = '#^((0?[1-9])|([1-2][0-9])|(3[01]))[/\\.-_ ]((0?[1-9])|(1[0-2]))[/\\.-_ ]([0-9]{4})$#';
   const alpha_pattern = '#^[a-z]+$#i';
   const alnum_pattern = '#^[a-z0-9]+$#i';
   
   
   public static function email($email) {
      return (preg_match(self::email_pattern, $email)) ? TRUE : FALSE;
   }
   public static function frenchPhone($number) {
      return (preg_match(self::fr_phone_pattern, $number)) ? TRUE : FALSE;
   }
   public static function url($url) {
      return (preg_match(self::url_pattern, $url)) ? TRUE : FALSE;
   }
   public static function date($date) {
      return (preg_match(self::date_pattern, $date)) ? TRUE : FALSE;
   }
   public static function alpha($string) {
      return (preg_match(self::alpha_pattern, $string)) ? TRUE : FALSE;
   }
   public static function alnum($string) {
      return (preg_match(self::alnum_pattern, $string)) ? TRUE : FALSE;
   }
}

class MySQL {
   private $conn;
   private $db;
   private $username;
   private $password;
   private $host;
   private $port;
   
   public function __construct($host, $user, $pw, $db, $port = '3306') {
      $this->username = $user;
      $this->password = $pw;
      $this->host     = $host;
      $this->port     = $port;
      $this->db       = $db;
      
      $this->conn = mysql_connect($this->host . ':' . $this->port, $this->username, $this->password);
      mysql_select_db($this->db, $this->conn);
   }
}

class HtmlHelper {
   
   /* New tag
    *
    * @created : 8.7.2011
    * @function : open a new tag (not self closing), add classes and an ID
    */
   public static function newTag($type = 'div', $classes = NULL, $id = NULL) {
      $return     = '<' . $type;
      if(is_string($id))
         $return .= ' id="' . $id . '"';
      if(is_string($classes))
         $return .= ' classes="' . $classes . '"';
      $return    .= '>';
      return $return;
   }
   /* Close tag
    *
    * @created : 8.7.2011
    * @function : close any non self closing tag, add structure to the HTML page by adding information about which tag is being closed
    *
    * $info : the id or class of the tag that is closed, for example '.myCustomClass' or '#myCustomId'
    */
   public static function closeTag($type = 'div', $info = NULL) {
      $return     = '</' . $type;
      if(is_string($info))
         $return .= '<!-- /' . $info . ' -->';
      $return    .='>';
      return $return;
   }
   /* Meta tag
    *
    * @created : 8.7.2011
    * @function : add any meta tags that have a name and a content attribute    
    */
   public static function metaTag($name, $content) {
      $return = '<meta name="' . $name . '" content="' . $content . '" />';
      return $return;
   }
   /* Title of the HTML page
    *
    * @created : 8.7.2011
    */
   public static function title($title) {
      $return = '<title>' . $title . '</title>';
      return $return;
   }
   /* Favicon
    *
    * @created : 8.7.2011
    * @function : add a favicon to the page
    * @filetype: PNG
    */
   public static function favIcon($filePath) {
      $return = '<link rel="shortcut icon" type="image/png" href="' . $filePath . '" />';
      return $return;
   }
   /* CSS
    *
    * @created : 8.7.2011
    * @function : add a new Cascading StyleSheet to the current page, and set the media it should apply to
    * @filetype : CSS
    */
   public static function css($filePath, $media = 'all') {
      $return = '<link rel="stylesheet" type="text/css" href="' . $filePath . '" media="' . $media . '" />';
      return $return;
   }
   /* JS
    * @created : 8.7.2011
    * @function : add a new Javascript file to the current page, and choose whether it should defer or not
    * @filetype : JS
    */
   public static function js($filePath, $defer = FALSE) {
      $return     = '<script type="text/javascript" src="' . $filePath . '"';
      if($defer === TRUE)
         $return .= ' defer="defer"';
      $return    .= '></script>';
      return $return;
   }
}

?>
<!DOCTYPE HTML>
<html lang="en" dir="ltr">
<head><?php
echo HtmlHelper::title('ConradK - PHP and webdesign amongst other things');
echo HtmlHelper::favIcon('favicon.png');
echo HtmlHelper::metaTag('keywords','ConradK, Conrad Kleinespel, Webdesign tutorials, PHP tutorials, Free PHP code');
echo HtmlHelper::metaTag('description','I make things. I like PHP. If I could marry someone, it would probably be a beautiful Regex :)');
echo HtmlHelper::css('styles.css', 'screen, print');
echo HtmlHelper::js('custom.js',TRUE);
?></head>
<body>

<?php

?>

</body>
</html>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

I will add them tomorrow, well look at them tomorrow really, looks good to me though :D
Image
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

Re: BetterPHP PHP Library

Post by conradk »

jacek wrote:I will add them tomorrow, well look at them tomorrow really, looks good to me though :D
Great :) I will be updating them of course. I made these today, but still there is a lot to do. More tomorrow.
User avatar
DomC
Posts: 91
Joined: Mon Jul 18, 2011 1:58 pm

Re: BetterPHP PHP Library

Post by DomC »

Hey,
If you still need it, ill be glad to help.
Although I do not know much.

Dom
I can't think of anything witty to put here!

Check out some of my projects on GitHub: https://github.com/DomTC
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

If you have somethign to add that could be useful feel free to post it.
Image
User avatar
DomC
Posts: 91
Joined: Mon Jul 18, 2011 1:58 pm

Re: BetterPHP PHP Library

Post by DomC »

Ok, but do you have any particular requests?
I can't think of anything witty to put here!

Check out some of my projects on GitHub: https://github.com/DomTC
User avatar
DomC
Posts: 91
Joined: Mon Jul 18, 2011 1:58 pm

Re: BetterPHP PHP Library

Post by DomC »

Would some logging scripts be good?
Such as visitors, errors, 404's, hits etc.

If that's wanted i'll get started
I can't think of anything witty to put here!

Check out some of my projects on GitHub: https://github.com/DomTC
User avatar
Linkjuice57
Posts: 23
Joined: Tue Jul 12, 2011 2:44 pm

Re: BetterPHP PHP Library

Post by Linkjuice57 »

DomC wrote:Would some logging scripts be good?
Such as visitors, errors, 404's, hits etc.

If that's wanted i'll get started
Well, I'd personally like to see that. Could learn something from it :-)
non-existent PHP newb
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BetterPHP PHP Library

Post by jacek »

DomC wrote:Would some logging scripts be good?
Such as visitors, errors, 404's, hits etc.

If that's wanted i'll get started
Linkjuice57 wrote:Well, I'd personally like to see that. Could learn something from it :-)
Looks like you have your answer to that ;)

I have no requests but this is a community thing so, literally anything.
Image
Josh
Posts: 38
Joined: Tue Oct 11, 2011 9:31 pm

Re: BetterPHP PHP Library

Post by Josh »

I'd suggest using the
__autoload($class_name) {
include $class_name . '.php';
}
function rather than doing that include all, including them all will slow page speed which isn't good, especially if they're not all being used.

Oh and I'd suggest you look at CodeIgniter's docs and helpers, their helpers are usually really useful (well, they were when I used CI).
Post Reply