Very Simple php login

This isn’t pretty I know and I will probably clean this up and add to it. It literally took me less than 5 minutes to write this. I just wanted to create a very simple and basic authentication/login system just to show how easy it is to get started creating a login in php.

So here goes it!

The index.php file:

The auth.php file:

More on this as I get time to do so. Leave your comments below, maybe some ideas on what should be included? I will leave that to you (the reader). Thanks!

Posted in Uncategorized | Comments Off

Mad Natter Template Library for Codeigniter

If your looking for a really simple to use library for templating your codeigniter application, I have one for you. I call it “Mad Natter” (I dont why?) :) . Anyway it’s a fairly simple library for templating your next CI App.

This library doesn’t have all the “bells” and “whistle” that many of the other templating libraries may have, but it does get the job done. And it does it with out having to modify a bunch of code. Just drop it in and start using it!

You can check it out over on GitHub

So how do we use it you ask? Well let me show you!

Through out this tutorial I will assume you are using the library as is with out changing any of the default properties in the template.php file. If you have you will need to adjust the code in this tutorial accordingly.

Setup the Library

By default the library assumes a coulpe things, first is that all your css and js files are in a folder off your root, labeled “assets” and that the base template files are located in “application/views/template/” folder

If your directory structure is different dont worry, we can change that. Open up the Template.php located in “/application/libraries/Template.php”. If you haven’t downloaded a copy head over to github and grab a copy, I’ll wait.

Ok, so you have it? Good, crack open the Template.php located in “/application/libraries/” and you should see something simalar to this:

So what now? Well as you can see we have a set of predefind properties that allow us to specify where are template files are located. Dont worry if you dont understand right away, we will go into more details on these properties further in this post.

This is the directory for your template files and should be in the “application/views/” folder.

These properties are used to set the default files to use for your template. These file should be in the directory specified in $template_dir. (i.e. application/views/template/index.php)

This is used to to set the title of your page in the head of the document between the “<title></title>” tags. You will be able to use it like this:

This will be the property that holds the view we will want to load.
This is to set a view by default, this file should be in you CI views folder.

This is the directory where all your js and css files are.

No need to change these, they are just place holder for data we will be sending the library.

Ok now that we have evrything set we can actually use it!

Loading the template library

If you downloaded the source from GitHub then it should be a working copy of CI with the template library already loaded. If not then you can read on to see how to load the library and use it in a controller.

In the controller:

Autoload: (application/config/autoload.php)

Using the library in your controller.

Now that we have the “Mad Natter Templater” library loaded and we are passing data to it and it’s returning data, how do we use it? Simple you already are!

We just havent displayed any data yet or maybe yours already is? Either way lets have a look and see what we have.

So what did we do? Well we loaded the library, we sent the library a paramater “welcome_message”, this is the “view” we want the template library to load for us and it returned an object with all our template data in a varaible called $template_data. Awesome! right?

Note: welcome_message should be a php file in your “application/views/” folder.

Ok, so now what to do with that data? Simple, we just need to tell CI to load the view file that we just passed to the template library and our template.

To do that we simply do this:

Now that we have our controller loading our template we need to open up the template files to see what we have. Go ahead and open up the file index.php in “application/views/template/index.php”, you should have something simalar to this:

Here you can see a little better whats going on, we have told CI to load the template file, which is “application/views/template/index.php” and passed the extra template data to that file.
Now we can just load our header and footer views as seen above and our view as well. The $view varaible now hold the paramater “welcome_message” that we passed to the template load() function earlier. Remeber?

Let’s open up one more file, the header.php in the template folder.
You should have something like this:

Here we just echo out any css, js or meta info that we pass to the template library. All though we havent passed anything yet, you can read below how to do that.

Starting to make more since now? Good! If not just keep playing with it and check out more of whats going on inside the template.php library file, you’ll get the hang of it!

Well thats it! Wait that cant be all? Your right! There is more but thats the basics.

Other Functions

These are some of the other functions in the library, available to use in you controller and how to use them. These function should be called before the $ths->template->load(); function.

Note: you will need to specify the absolute url if using the second param.

Local file in the “/assets/css/” directory

Notice that there isn’t a need to specify the extension (i.e. “my_css.css”)

Loading a remote file or file with different extension

Adding extra attributes

To add extra attributes to the link tag for you css file you can use an array() or an object.

Will produce:

Adding Javascript

Adding Java script is exactly the same as adding css

Adding Meta

Adding meta is as simple as passing an array or object to the function

Will produce:

Or with an object

Will produce:

For more useage examples check out the README over on GitHub.

Posted in Codeigniter | Tagged , , , | Comments Off

Codeigniter custom 404 page and show_404 function

If your looking to cusomize the default 404 page thats displayed when a user enters an invalid URL, with your codeigniter application, there are a few different way in which you can do this.

Codeigniter Default 404

Codeigniter Default 404

So your ready to modify your default 404 page for your codeigniter application?

error_404.php
To modify the default page open up the default error_404.php in your faviorte text/html editor and make the changes you would like.

The default 404 file is located at “/application/errors/error_404.php”

If your looking to get a bit more creative and wrap your 404 page in a template we can do this by extending CI_Exceptions.

First, create a file in your “/applications/core/” directory labeled “MY_Exceptions”, unless you have changed the default subclass_prefix in the “/applications/config/config.php” file, by default it should look like this:

Take notice that “MY_” is uppercase, the file you created in the core directory will need to reflect this exactly. Your file should be labeled exactly as shown: “MY_Exceptions.php”

Again if you have changed this prefix in your config file you will need to label your file to reflect the same prefix, i.e.

Would be: “YouR_Exceptions.php”

Now open that file in you text editor and add the following code:

If your curious about the ” $CI =& get_instance(); “ you can read more about that in the codeigniter manual here:
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

I must also point out that in the above script im using a template file located in “/includes/template/” and am passing data to the file. the below is an example of how this file might look.

Your template file may be a little different. The main thing to notice here is the variable “$main_content” . This is the variable passed containing the path to our error_404.php page in our MY_Exceptions.php file.

Now create a directory in your “/application/views/” labeled “errors” and create a file labeled “error_404.php”.

Now open that file and place the following code inside and save it.

Now in your controller all you have to do is call the show_404() function like so:

Enter an invalid URL and you should now see your new custom 404 page loaded in your template.

Custom 404 Page with Tempalte

Custom 404 Page with Tempalte

Posted in Codeigniter, PHP | 30 Comments

Codeigniter and Query Results MySQL Error 1064

Although the title refers to Codeigniter this applies to anytime you run a query and get the MySQL Error 1064.

If your trying to Query results with MySQL and get and Error 1064 more than likely you forgot to escape something in your database results.

Take the following:

In Codeigniter you might run a query like this:

Everything may look ok but in fact you forgot one importint thing! To escape the value you querying, the email address in this case.

Your code should look like this:

Notice the “.$this->db->escape($email).” ? Granted this is a specific way to query for Codeigniter however, your query statement should always use an escaped value when dealing with emails or other strings that might need escaping.

An alternative way of escaping characters with Codeigniter is using bindings. Take the following for example:

Here we have used a question mark as a place holder and placed our values in an array. By doing this Codeigniter will automatically escape the values.

If your using something other than Codeigniter you can simply use the following:

mysql_real_escape_string(); Note: Use of this extension is discouraged.

Instead you should be using one of the following:

mysqli::real_escape_string();
or
php’s PDO_MYSQL

Posted in Codeigniter | Tagged , , , | Comments Off