Category Archives: Cake Php

Session locking when using multi AJAX request

What session locking is

To understand this, we first need to know that a web server does not run your PHP code in a single process. Multiple worker processes are running concurrently and they are all handling requests. Normally, visitor requests of your web page are serialized. This is also where HTTP persistent connections (a.k.a. keep-alives) come into play. By keeping the connection open for the requesting of all the assets of the page, the connection overhead is avoided. Browsers are quite smart and will always try to serialize requests for HTML pages. For the assets (images, scripts, etc.) on the page there is another strategy. The browser will download multiple assets in parallel from each unique hostname it sees referred in the HTML. It can do this by opening multiple TCP connections or by pipelining. When a browser thinks it is downloading assets it may download these for a single visitor in parallel. Session locking avoids this parallelism (by blocking) to provide reliable access to the session data in this situation.

How session locking works

This is quite easy: When you call “session_start()” PHP will block (wait) in this call until the previous script has called “session_write_close()”. On Linux it does this by relying on the “flock()” call. This is an advisory locking mechanism that blocks until the lock is released. NB: This locking time is not counted as part of the “max_execution_time” (see:set_time_limit()).

Why session locking is necessary

Session locking prevents race conditions on the shared memory that is used to store session data. Every PHP process reads the entire session storage when starting and writes it back when closing. This means that to reliably store the logging-in of a user (which is typically done in the session data) you must make sure no other process has read the session data and will overwrite your data after you have written it (since the last write wins). This is needed even more when using AJAX or IFrames since the browser considers those loads to be assets and not HTML pages (so they will be parallelized).

Source :


CakePhp + Facebook, Google login API

I. Facebook

  1. Jquery

2. Controller

3. Html

II. Google +

  1. Jquery

2. Controller

3. Html

Jquery validate ajax check duplicate email

I. Jquery

II. Controller


Quick Start With Cake PHP 3

I. Install IDE for PHP

  • Download PHPStorm 10.0.3 – Key PhpStorm-Key
  • Keymap Scheme : Eclipse
  • IDE Theme : Dracula
  • Colors and Fonts : Dracula
  • You cant edit keymap follow : File -> Settings -> Keymap -> change what you want.

Display *.ctp as PHP file :

II. Install Composer on Window via Composer-Setup.exe

III. Create new project using Composer

Edit file xampp/php/php.ini and remove “;” at extension=php_intl.dll

Right Click -> Use Composer Here

IV. Command using in composer

  • Remove dependencies : composer remove facebook/php-sdk-v4 –update-with-dependencies
  • Create project

  • Generate add,edit,delete,view sites of table in database

V. Using template HTML/CSS/JS

  • In AppController, function beforeRender we need set template before open site.


  • In template.ctp only keep header and footer. We must replace contain follow code:
  • Change link CSS and JS, Image follow code:

VI. Multi-language

  • In Cake3, We using I18n Shell to translate language
  • Link

VII. Admin Plugin and Routing

  • In config/router.php

  • Create folder Admin in /src/Controller and Admin in /src/Template. .
  • Copy AppController and TestController into folder /src/Controller/Admin. Remember change namespace to App\Controller\Admin.
  • Create folder Test in /src/Template/Admin. Create index.ctp in folder Test. Content of file index.ctp : <h1> Hi index </h1>.

VIII. Associations (Relationship in Cake)

Link :

  • In parent table , function init :

  • In child table, function init:

  • How to get list child from parent

IX. jQuery/Javascript to replace broken images

Handle the

event for the image to reassign its source using JavaScript:

Add event on img tag

Or without a JavaScript function:

The following compatibility table lists the browsers that support the error facility:

X. Ajax function cannot write Cookie

First thing, you should enabled debug mode in your application (

), or check the debug logs, then you’d see that you are getting a “Cannot modify header information – headers already sent” warning.

Do not manually output data using or the like

Except for session cookies which are being sent immediately when a session is started, normal cookies are queued in the

object (

in your controller) until after the controller action has been executed, so doing an

in your controller action will cause headers and data to be sent, and therefore the cookie headers cannot be sent anymore.

Use a view or the object instead


using reCaptcha in cake php

  1. Go to register your website and get key.
  2. Client-side integration
    Paste this snippet before the closing </head> tag on your HTML template:

    Paste this snippet at the end of the <form> where you want the reCAPTCHA widget to appear:
  3. Server side integration
    When your users submit the form where you integrated reCAPTCHA, you’ll get as part of the payload a string with the name “g-recaptcha-response”. In order to check whether Google has verified that user, send a POST request with these parameters:

    secret (required) your secret key
    response (required) The value of ‘g-recaptcha-response’.
    remoteip The end user’s ip address.
  4. Controller