Reading and Opening a File in PHP

PHP readfile() Function

The readfile() function reads a file and writes it to the output buffer.

PHP Open File – fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

PHP Create File – fopen()

The fopen() function is also used to create a file.

$myfile = fopen("example.txt", "w")

Yii and Bootstrap – YiiStrap: How to Install and Configure

Simple steps to help you get started with YiiStrap.

  1. Install Yii.
  2. Create a new Yii application.
  3. Copy YiiStrap.
  4. Change configuration.
  5. Use Gii: Automated Code Generation.
  6. Create CRUD using Gii

Install Yii

  1. Download the latest Yii Framework from here: http://www.yiiframework.com/download/
  2. Unzip it into your htdocs or www or public_html folder. Basically unzip it into a browse-able location.
  3. You need only the framework folder. All other folders are optional. Keep the requirements folder if you want to ensure that your web-server has all the requirements for Yii to work.

Create a new Yii application.

  1. Open a terminal window.
  2. Go into the framework directory
  3. Run this command:

    ./yiic webapp ../myProjectName
  4. The new Yii application will be created at “[the path to the folder where the framework folder is located]/[myProjectName]”.
    You can access it with the following URL: http://mydomain/myYiiPath/myProjectName/index.php

Copy YiiStrap.

  1. Download the latest YiiStrap extension from here: http://www.getyiistrap.com
  2. Create a new folder called “bootstrap” in your new Yii applications protected/extensions folder
  3. Unzip the YiiStrap zip into this new older

Change configuration.

  1. Open your new Yii applications protected/config/main.php file
  2. Add this to the beginning of the file:
    Yii::setPathOfAlias(‘bootstrap’, realpath(__DIR__ . ‘/../extensions/bootstrap’));
  3. Add these to your return array:
    // path aliases
    ‘aliases’ => array(
    ‘bootstrap’ => realpath(__DIR__ . ‘/../extensions/bootstrap’), // change this if necessary
    ),
    ‘import’=>array(
    ‘application.models.*’,
    ‘application.components.*’,
    ‘bootstrap.behaviors.TbWidget’,
    ‘bootstrap.helpers.*’,
    ‘bootstrap.widgets.*’,
    ‘bootstrap.helpers.TbHtml’,
    ),
    ‘modules’=>array(
    // uncomment the following to enable the Gii tool
    ‘gii’=>array(
    ‘class’=>’system.gii.GiiModule’,
    ‘password’=>’admin123′,
    // If removed, Gii defaults to localhost only. Edit carefully to taste.
    ‘ipFilters’=>array(‘127.0.0.1′,'::1′),
    ‘generatorPaths’ => array(‘bootstrap.gii’),
    ),
    ),

    ‘components’=>array(
    . . . . . . . . .
    . . . . . . . . .
    ‘bootstrap’ => array(
    ‘class’ => ‘bootstrap.components.TbApi’,
    ),

    ‘db’=>array(
    ‘connectionString’ => ‘mysql:host=localhost;dbname=pmtool’,
    ’emulatePrepare’ => true,
    ‘username’ => ‘jessie’,
    ‘password’ => ‘jessonita’,
    ‘charset’ => ‘utf8′,
    ),
    . . . . . . . . .
    . . . . . . . . .
    ),

Use Gii: Automated Code Generation.

Use the Gii Automated Code Generator to create CRUDs for your tables. The tools is pretty straight forward. Here is the URL:
http://myDomain/myYiiPath/index.php?r=gii/

That’s it! Open the model, controller and view folder in the protected folder to see how they are written.

Get the Full Benefits of YII framework through Training

PHP is now a very strong scripting language, often used in server-based or cloud environments, especially with the now strong frameworks it has. Still, many developers find working with new frameworks like YII, somewhat of a challenge. This is when a good training course can help. Of course, as a developer you can probably figure out how to find your way around in the language. Still, a good course can save you time, keep you from creating lots of bugs and then having to go back in and rework them. A quick and useful course can often save you months of headaches and problems.

YII

What is YII?

Currently, YII is one of the most popular frameworks used by web developers when creating Web 2.0 applications. The Model View Controller (MVC), DB migration, form input and validation make this an essential framework when working with PHP. YII helps developers work with the various theme features, layout files, images and CSS.

YII allows developers to create many different applications and web functions easily, especially for server-based projects. But Yii has a few other benefits as well, and many of these may not be so well known. That’s why specialized training courses may be a good idea.

Security and Speed is What It’s All About

The YII framework offers speed, security and organization, which are all aspects that developers are looking for as this gives added flexibility. In fact, recent studies show that many PHP sites are developed using YII.

The Comparison

When you compare YII to other frameworks like Zend, CakePHP, Codeigniter and others YII always tends to stand out because of its model structure which makes the website loading process much faster.  In fact, Yii currently holds the bench mark of 673 requests per second, if combined with the Alternative PHP Cache. On it´s own, it still remains fast, processing at close to 93 RPS.

High Security

YII is also incomparable when it comes to security. It is virtually impenetrable and has Cross site Request Forgery Prevention, Cross site Scripting Prevention and Cookie Attack Prevention tools. It also has a Server Side Validation facet. All of this protection makes it one of the most secured frameworks in existence.

YII and Caching

A frequent problem with web-based applications is slow caching, but now, the Yii framework takes care of that. YII allows the developer to use cache components and store them in separate media, making web applications easier and faster to load.

YII Also Known for Its Compatibility With Third Party Code

Developers are also using the YII framework efficiently with third party codes. Those using PHP, find that the integration of this framework with other 3rd party frameworks works well. Still, getting the full benefits of YII early on can be a challenge, especially when you don’t know the “ins and outs” of the problem. That’s why effective training courses could help you command YII fully within a few short months, thus helping you avoid all the pitfalls that come with learning a new programming language.

osCommerce Display Errors on Payment Page

You will notice that checkout_payment.php has a line like this which allows osCommerce display errors from your modules on the payment selection page. Interestingly, this is the only script in the checkout process that has a line like this – wonder why? Anyway, back to the point.

<?php 
if (isset($HTTP_GET_VARS['payment_error']) && is_object(${$HTTP_GET_VARS['payment_error']}) && ($error = ${$HTTP_GET_VARS['payment_error']}->get_error())) { ?&gt;
 
<div class="contentText"> &lt;?php echo '<strong>' . tep_output_string_protected($error['title']) . '</strong>'; ?&gt; 

<p class="messageStackError">&lt;?php echo tep_output_string_protected($error['error']); ?&gt;</p> 

</div> 

<?php } ?>

Lets break down the if statement and look at each condition.

This condition is clear (if not, it basically checking if the variable is exists)

isset($HTTP_GET_VARS['payment_error'])

What about this, next condition?

is_object(${$HTTP_GET_VARS['payment_error']})

And this one?

($error = ${$HTTP_GET_VARS['payment_error']}->get_error())

How or why is it checking for an object in a GET variable? How am I supposed to do that? I just want to display the error string and its checking for an objects!
It took me a while of Googling (and loss of hair) to find out how to display an error in osCommerce.

From anywhere in your module, when you want to redirect back to the payment selection page (of the checkout process) with a custom error, do this:

tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=[YOUR CLASS NAME]&[CUSTOM PARAMS TO HELP YOU IDENTIFY THE TYPE OF ERROR]', 'SSL'));

Next, in your module class have a function like this:

function get_error() {
    global $HTTP_POST_VARS, $HTTP_GET_VARS;

    [YOU CAN ACCESS YOUR CUSTOM PARAMS HERE TO DECIDE WHAT ERROR MESSAGE TO DISPLAY]

    $error = array('title' => '[A TITLE FOR THE ERROR MESSAGE',
                   'error' => '[THE ERROR MESSAGE]');
}

Now, the 3 conditions in checkout_payment.php will be clear.

  1. The first condition checks if, payment_error GET variable exists.
  2. The second condition checks if, there is a class matching the name in payment_error.
  3. The third condition checks if, that class has a method get_error() which has a non empty return value.

That’s it! Hope this helps speed up your development process.

Using Aweber Params Shortcode – Schogini Plugin in WordPress

The use of Aweber Params Shortcode by Schogini in wordpress is simple. This plugin is used to get the user details like name or email from the Aweber signup form and use it inside your redirection pages. This can be accomplished by using short codes.

 

The short code which will embed the name parameter is . If you want to embed the email parameter, then you have to use the short code, .

 

Also you have to add the redirection page URLs (where you want to embed the shortcode) in the Aweber Params settings section. Here you have the option to add at least 5 URLs. While adding the URL’s you have to make sure that you are adding the page url with out the domain name as its part. e.g. : confirmation-page/ , if your URL is http://www.domain.com/confirmation-page/)

 

You have to make sure that you use these short codes in the text mode and not visual mode of the wordpress page or post.

That’s it. You will find the plugin here : http://wordpress.org/extend/plugins/aweber-params-shortcode/

How to create login page using Http authentication in php

The following code snippet will create an http authentication for your script:

$login = ‘admin';
$pass = ‘admin123′;

if(($_SERVER[‘PHP_AUTH_PW’]!= $pass || $_SERVER[‘PHP_AUTH_USER’] != $login)|| !$_SERVER[‘PHP_AUTH_USER’])
{
header(‘WWW-Authenticate: Basic realm=”Test auth”‘);
header(‘HTTP/1.0 401 Unauthorized’);
echo ‘Login failed';
exit;
}