Archive for 'PHP'

CSS files and $scripts_for_layout

You probably know that if you have $scripts_for_layout in your site’s layout, then you can include JS files from the view. For example, if you’d like to include ‘myScript.js’ from some view you would add the following code:

Read more

Cakephp – Beautify Html Code Output

It’s no secret that CakePHP often outputs some very ugly and hard to read HTML. This little trick will make it a lot cleaner.
Create a file named app_helper.php in your app’s root directory.

Read more

CakePHP – Models Part 2

Callback Methods

If you want to sneak in some logic just before or after a CakePHP model operation, use model callbacks. These functions can be defined in model classes (including your AppModel) class. Be sure to note the expected return values for each of these special functions.

Read more

CakePHP – Models Part 1

Naming

  • Ingredient
  • extends AppModel
  • ingredient.php

The Model is automatically available to the Controller when the name matches

function index() {
//grab all ingredients and pass it to the view:
$ingredients = $this->Ingredient->find(‘all’);
$this->set(‘ingredients’, $ingredients);
}


Read more

CakePHP – Components

Naming:

  • MathComponent
  • extends Object
  • /app/controllers/components/math.php

Initialising:

var $components = array(‘Math’, ‘Session’);

When including Components in a Controller you can also declare a set of parameters that will be passed on to the Component’s initialize() method. These parameters can then be handled by the Component.
var $components = array(
‘Math’ => array(
‘precision’ => 2,
‘randomGenerator’ => ’srand’
),
‘Session’, ‘Auth’
);


Read more

CakePHP – Controllers

Naming conventions:

  • RecipesController
  • recipes_controller.php
  • extends AppController

Different Variables

var $name = ‘Recipes’;
var $uses = array(‘Recipe’, ‘User’);
var $helpers = array(‘Ajax’);
var $components = array(‘Email’);

Read more

How to Use Cookies in PHP

What is a Cookie?

A cookie is flat file based system used to represent a user of the website. It is stored on the local computer of the user. When you visit a website a cookie may be set to represent the user. Next time he visits the website, he does not need to identify himself manually; instead the cookie will represent him on that website. With the help of PHP, cookies can be created, read and destroyed.

Why do we use Cookies?

We can literally put 4000 characters of data in a flat cookie file and store information about the user preferences for a particular website. Some of the practical uses of Cookies are as follows:

  • Many sites use them to provide customized pages and results to their users. This can be achieved by storing all the information like preferences etc in a cookie.
    .
  • Many websites use cookies to log their users in automatically. By storing a few pieces of user information they can automatically authenticate the user’s details and use them to save the user time when they log in.
    .
  • Visitor tracking and statistics systems often use them to track visitors. By assigning the visitor a cookie, they will not be counted more than once, so accurate unique visitor statistics can be obtained.

Cookie Security:

Practically there is no security threat while using cookies. A cookie set by a particular website cannot be accessed or even check if it exists by another website even if it wants to. But since cookie is just a flat text file, it can be opened and read on the computer it is stored in.

If a website has stored a password in a cookie it can be read and this can pose threat to hacking. But if the same password is encrypted using a hash like md5() or sha1() then it can be more secure since this content is used to match it with the password stored on the website.

To read the full article i have written please visit: CLICK HERE