PHP Arrays

PHP arrays allows you to store multiple values into 1 variable. In PHP, you can create arrays in many ways. Lets take a look at few ways:

 
// Create an empty array named $people
$people = array();
 
// Create an array with some values 
$people = array('Eddie','Mike','Chris');
 
// Same as above, but wrote differently
$people[]='Eddie';
$people[]='Mike';
$people[]='Chris';

Whats great about arrays is that you can also put arrays inside of other arrays making the structure unique to your needs. Arrays inside of other arrays are called Multidimensional Arrays. Here is an example of a multidimensional array named $people storing their names into different groups:

$people = array(
                'group_1' => array('Mike','John','Jim'),
                'group_2' => array('Mel','Chris','Molly','Jane'),
                'group_3' => array('Eddie','Dennis','Paul'), 
               );

If we look above, we see that we have 3 groups with the value of an array all stored inside of the $people array. In order to access the information, you can use either a loop (for loop or a for each loop) or you access it directly if you know the path to it. Lets take a look at how you would do it both ways:

 
// access the group_1 array directly
$people['group_1']
 
//to access the first element in the array in group_1
$people['group_1'][0]
 
 
//print everything out from the group_1 array using a loop
foreach($people['group_1'] as $name)
{
     echo $name."<br>";
}

how to remove the .svn folder from your project

If your looking to get rid of the .svn folder from you project, there are 2 quick and easy ways to do so:

  1. The easiest way to export your SVN project to another folder. If you have TortoiseSVN installed, you can easily do this by right clicking on your project and choosing to export it.
  2. If option 1 is not possible, you can run an rsync command and set a flag to omit a folder. Here is an example:
    rsync -avz --exclude '.svn' path_to_folder_with_.svn/ destination/

If you know any other ways of doing it just as quick, I’d love to hear from you in the comments section! :)

Simple Codeigniter Authentication using XML (no database needed)

In this example, I am going to show you how you can use an XML file which contains users to authenticate or secure a specific part of your website. Lets first create our XML file :

<?xml version="1.0" encoding="utf-8"?>
 
<users>
	<user>
		<id>4334</id>
		<name>Mike</name>
		<email>mike@someone.com</email>
		<password>test</password>
		<permissions>
			<item>read,write,delete</item>
		</permissions>
	</user>
 
	<user>
		<id>23452</id>
		<name>Molly</name>
		<email>molly@someone.com</email>
		<password>test2</password>
		<permissions>
			<item>read,write,delete</item>
		</permissions>
	</user>
 
	<user>
		<id>234234</id>
		<name>James</name>
		<email>james@someone.com</email>
		<password>test3</password>
		<permissions>
			<item>read,write,delete</item>
		</permissions>
	</user>
 
 
 
</users>

Lets name this XML file “users.xml”. I’ve placed this document in my document root under /_assets/auth/users.xml.

Now, lets create a simple login form. in your application/views folder, create a file named login_form.php and add the following:

<form action="<?=base_url()?>auth/login" method="post"><label for="email">Email Address</label> <input type="email" name="email" />
 
<label for="password">Password</label> <input type="password" name="password" /> <input type="submit" value="login" />
 
</form>

Now, that we have our login form, we can create the controller. Our controller looks like this:

<?php
class Auth extends CI_Controller 
{
    public function __construct() 
	{
        parent::__construct();
		session_start();
                $this->load->helper('url');
 
    }
 
	public function index()
	{
		if($this->is_logged_in() == FALSE)
		redirect('auth/login');
		else redirect();
	}
 
	public function is_logged_in()
	{
		if(@$_SESSION['is_logged_id'] == true)
		return true;
		else return false;
	}
 
	public function login()
	{
		if($this->is_logged_in()) redirect();
 
		$this->load->library('form_validation');
		$this->form_validation->set_rules('email', 'Email', 'required|callback_validate_login');
		$this->form_validation->set_rules('password', 'Password', 'required');
 
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('login_form');
		}
		else
		{
			// Set Session
			$e = $this->input->post('email',true);
			$p = $this->input->post('password',true);
			$u = $this->get_users();
			$_SESSION['is_logged_id'] = true;
			foreach( $u[$e] as $k => $v )$_SESSION[$k] = $v;
			redirect();
		}		
	}
 
	public function validate_login()
	{
 
		$e = $this->input->post('email',true);
		$p = $this->input->post('password',true);
 
		$u = $this->get_users();
 
		if(	is_array($u[$e]) )
		{
			if( $u[$e]['password'] == $p )
			{
				return true;
			}
			else
			{
				$this->form_validation->set_message('validate_login', ' Your email/password is incorrect. Please try again');
				return FALSE;
			}
		}
		else
		{
				$this->form_validation->set_message('validate_login', ' Your email/password is incorrect. Please try again');
				return FALSE;
		}
 
 
	}
 
 
	public function view_session()
	{
		echo "<pre>";
		print_r( $_SESSION );	
	}
 
	public function logout()
	{
		session_destroy();
		redirect('auth');	
	}
 
 
	public function get_users()
	{
		$this->load->helper('file');
		$users_string = read_file('_assets/auth/users.xml');
		$users_array = array();
		$users = new SimpleXMLElement($users_string);
 
		foreach($users->user as $u)
		{
			$users_array[ (string)$u->email ]['id'] = (string)$u->id;
			$users_array[ (string)$u->email ]['name'] = (string)$u->name;
			$users_array[ (string)$u->email ]['email'] = (string)$u->email;
			$users_array[ (string)$u->email ]['password'] = (string)$u->password;
 
			foreach($u->permissions as $p)
			{
				@$users_array[ (string)$u->email ]['permissions'] = explode("," ,(string)$p->item);
			}
 
		}
 
		return $users_array;		
	}
 
 
 
 
 
 
}
SSH

Making WGET not save the page

Wget is a free package that runs on most UNIX-like operating systems as well as Windows. It is used for retrieving files using HTTP, HTTPS, FTP and other widely-used internet protocols. This commandline tool is non-interactive so it can be easliy called from scripts, cron jobs or terminals.

Basic syntax works like this:

wget http://www.example.com/get_this_file.zip

For cron jobs, downloading the file is not usually an option. Here is how you can set it to not save any files, but load the file or run it when needed.

wget -v -r -nd --delete-after http://www.example.com/run_this_file_but_dont_save.php
codeigniter-lg-299x300

Removing ‘index.php’ in CodeIgniter on Media Temple Grid Service (gs)

New to CodeIgniter?┬áHere is a quick look at how to setup your .htaccess file on Media Temple’s Grid Service (gs) platform to remove the ‘index.php’ from your CodeIgniter installation:

AddHandler php5-script .php
Options +FollowSymLinks
 
RewriteEngine on
RewriteCond $1 !^(index\.php|_assets|user_guide|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]