Skip to main content

Posts

Showing posts from 2014

MYSQLi PHP Basics without prepared statements

MySQLi and PDO are object oriented and support Prepared Statements (also support Transactions, Stored Procedures and more). Prepared Statements are very important for web application security, as they protect from SQL injection. Using Prepared Statements you do not have to escape strings before insert them in Database. Moreover, PDO offers support for many databases (not only MySQL). Establish a database connection $DBServer = 'localhost';  // or an IP address $DBUser   = 'root'; $DBPass   = 'your password'; $DBName   = 'dbname';  $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName); // check connection if ($conn->connect_error) { trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR); } //select rows $sql='SELECT * FROM users'; $rs=$conn->query($sql);   $rows_returned = $rs->num_rows; echo $rows_returned;  //returns number of rows //loop throu...

Yii Authentication and Authorization Login logout Access control filter and User Types

Authentication and Authorization check if a user is logged in or not via CWebUser::isGuest check if the user can perform specific operations by calling CWebUser::checkAccess  The main work in defining an identity class is the implementation of the IUserIdentity::authenticate method  class UserIdentity extends CUserIdentity {     private $_id;     public function authenticate()     {         $record=User::model()->findByAttributes(array('username'=>$this->username));         if($record===null)             $this->errorCode=self::ERROR_USERNAME_INVALID;         else if($record->password!==crypt($this->password,$record->password))             $this->errorCode=self::ERROR_PASSWORD_INVALID;         else         {             ...

PHP Static Methods and properties

Static Keyword Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can). For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public. Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static. Static properties cannot be accessed through the object using the arrow operator ->. Calling non-static methods statically generates an E_STRICT level warning. Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function retur...

PHP OOP Basics

Following a request by one of my blog readers, this is a very basic php class, actually there are two classes which should help demonstrate how easy it is to use object oriented PHP. Again. should you have any questions please use the comments or write to me using the form on your left ! The second class extends the first class, which basically means inherits all public methods of the first class. <?php //start class class MyClass { //define a property in the class public $prop1 = "I'm a class property!"; //define your functions public function __construct() {  echo 'The class "', __CLASS__, '" was initiated!<br />'; } public function __destruct() {  echo 'The class "', __CLASS__, '" was destroyed.<br />'; } public function __toString() { echo "Using the toString method: "; //or return $this->getProperty(); } public fun...

Yii Forms Basic Tips

Whether the model extends CFormModel or CActiveRecord, the important thing to remember is that the form is tied to a model. This is significant because it’s the model that dictates what form elements should exist, controls validation of the form data, and even defines the form’s labels (e.g., “First Name” for the firstName attribute), and so forth. When you use Gii to auto-generate CRUD functionality for a model, the framework creates a form for you in a file named _form.php. Any view file in Yii that starts with an underscore is intended to be an include. Naturally, the controller dictates which primary view file gets rendered. Also understand that the same _form.php file is intended to be used whether the form is for creating new records or updating existing ones. Yii will take care of populating the form’s elements with the model’s current value when an update is being performed. Because forms are normally tied to models, you’ll need access to a model instance when you go to crea...

Edit View Files in Yii Basics

In the HEAD, you’ll see that external files are linked using <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" /> Whenever you see Yii::app(), that refers to the Web application as a whole. You can access information about the user viewing a page, about the current or previously-viewed page, etc., there. Yii::app()->request specifically references the current page being accessed (or requested). The ->baseUrl part refers to the root URL for the application, like http://www.example.com. You should use Yii::app()->request->baseUrl for references to external files—CSS, JavaScript, images, and so forth—as the relative path to them can become muddled with the changed Yii URLs (like www.example.com/index.php/site/login). If you’re viewing a page like www.example.com/index.php/employee/view/id/1, which is intended to show the employee with an ID of 1, the actionView() method o...

Yii CHtml static class

Avaiable methods: CHtml::link() CHtml::ajaxLink() CHtml::button() CHtml::textField() CHtml::listData() CHtml::dropDownList() CHtml::link() method  public static string link ( string $text , mixed $url = ' # ' , array $htmlOptions = array ( ) ) Generates a hyperlink tag. Example 1: Linking to a controller action <?php echo CHtml :: link ( ' Link Text ' , array ( ' controller/action ' ) ) ; ?> HTML Output: < a href = " index.php?r=controller/action " > Link Text </ a > Example 2: Linking to a controller action with querystring parameters <?php echo CHtml :: link ( ' Link Text ' , array ( ' controller/action ' , ' param1 ' => ' value1 ' ) ) ; ?> HTML Output: < a href = " index.php?r=controller/action&param1=value1 " > Link Text </ a > Example 3: Linking to a control...