Saturday, March 3, 2012

Why dont you create your own CMS???--Part 9

In the previous post,we created our session class and user class.Here we will look into our 'actions.php' for handling actions like login,logout,register etc.The action to be performed will be decided from a url variable named 'action'.So whenever url variable action is set,corresponding action will be performed.

  • Open stupid >>> actions folder.
  • Create a file named 'actions.php'.
  • Open the file in an editor and write the following code.

global $user;
if (isset($_GET['action'])) {
    $action = $_GET['action'];
    switch ($action) {
        case 'login':
            $username = $_POST['uname'];
            $password = $_POST['pwd'];
            if (!empty($username) && !empty($password)) {
                if (!$user->login($_POST['uname'], $_POST['pwd'])) {
                    show_msg('Attention', 'Wrong username and/or password');
                } else {
                    if (is_admin()) {
                    } else {
            } else {
                show_msg("Attention", "All fields are required ");
        case 'logout':
        case 'register':
            if (!empty($_POST['username']) && !empty($_POST['pwd']) && !empty($_POST['email'])) {
                $data    = array(
                    'username' => $_POST['username'],
                    'email' => $_POST['email'],
                    'password' => $_POST['pwd'],
                    'level' => 1
                $user_id = $user->insertUser($data); //The method returns the userID of the new user or 0 if the user is not added
                if ($user_id == 0)
                    show_msg('Not Registered', 'User not registered'); //user is allready registered or something like that
                else {
                    show_msg('Registered', 'User registered with user id ' . $user_id . '');
            } else
                show_msg('Attention', 'ALL FIELDS REQUIRED');
            echo "no action";
Let us look into the details of the script in order.
  • Firstly,we declared the global variable $user which points to the object of the user class.
  • Checks whether the action isset using $_GET['action'] .
  • If set,the value is assigned to a variable $action.
  • Here the value can be login or logout or register.
  • We have used switch statement to perform the defined action.
Login action.

if the action to be performed is login,the post variables are checked.If the post varables are not empty,the following code is executed.
Remember the login method used in user_class.php.
The admin will be our first user i.e the user with user id 1.
BASE is our base url.
Similarly logout action as well as register action are carried out by their corresponding methods in the user_class.php.

The functions redirect_to(),is_admin(),show_message() will be added later to the functions.php.


Post a Comment