Monday, November 19, 2012

PHP switch statement in user management

PHP switch statement in user management

php switch statement can be used in user management First create database table  as bellow

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(60) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(60) NOT NULL,
  `pass_word` varchar(60) NOT NULL,
  `user_type` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
Then create bellow HTML file

<form id="loginForm" name="loginForm" method="post" action="check_login.php">
            <label>Username</label><br>
            <input size="30" name="user_name" type="text"><br>
            <label>Password</label><br>
            <input size="30" name="pass_word" type="password"><br>
            <label>User Type</label><br>
            <select name="user_type">
                                <option value="1">User1</option>
                                <option value="2">User2</option>
                                <option value="3">User3</option>
                
                                <option value="3">User4</option>
                                <option value="4">User5</option>
                                </select><br>
            <input value="Submit" type="submit">
</form>
Then create check_login.php

<?php
 
    session_start();
 
 
    require_once('dbcon.php');
 
 
    $errmsg_arr = array();
 

    $errflag = false;
 
 
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
 
    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }
 
 
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
 
 
    $user_name = clean($_POST['user_name']);
    $pass_word = clean($_POST['pass_word']);
   $user_type = clean($_POST['user_type']);
    //Input Validations
    if($user_name == '') {
        $errmsg_arr[] = 'Login ID missing';
        $errflag = true;
    }
    if($pass_word == '') {
        $errmsg_arr[] = 'Password missing';
        $errflag = true;
    }
   if($user_type == '') {
        $errmsg_arr[] = 'User Type missing';
        $errflag = true;
    }
    //If there are input validations, redirect back to the login form
    if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: login_form.php");
        exit();
    }
 
 
    $qry="SELECT * FROM users WHERE (user_name='$user_name' AND pass_word='".md5($_POST['pass_word'])."' AND user_type='$user_type')";
    $result=mysql_query($qry);
 
    switch($user_type){
case 1:
    //Check whether the query was successful or not
        if($result){      
    if(mysql_num_rows($result) == 1){
        //Login successful
            session_regenerate_id();
            $login = mysql_fetch_assoc($result);
            $_SESSION['SESS_username'] = $login['user_name'];
            $_SESSION['SESS_usercategory'] = $login['pass_word'];
            $_SESSION['SESS_password'] = $login['user_type'];
          
            session_write_close();
            header("location: upload1.php");
               exit();
            }else {
            //Login failed
            header("location: login_failed.php");
            exit();
            }
        }
       break;
     
case 2:
    //Check whether the query was successful or not
 if($result){      
          if(mysql_num_rows($result) == 1){
        //Login successful
            session_regenerate_id();
           $login = mysql_fetch_assoc($result);
            $_SESSION['SESS_username'] = $login['user_name'];
            $_SESSION['SESS_usercategory'] = $login['pass_word'];
            $_SESSION['SESS_password'] = $login['user_type'];
            session_write_close();
            header("location: upload2.php");
               exit();
            }else {
            //Login failed
            header("location: login_failed.php");
            exit();
            }
        }
     break;
   
case 3:
    //Check whether the query was successful or not
    if($result){      
    if(mysql_num_rows($result) == 1){
        //Login successful
            session_regenerate_id();
           $login = mysql_fetch_assoc($result);
            $_SESSION['SESS_username'] = $login['user_name'];
            $_SESSION['SESS_usercategory'] = $login['pass_word'];
            $_SESSION['SESS_password'] = $login['user_type'];
          
            session_write_close();
            header("location: upload3.php");
               exit();
            }else {
            //Login failed
            header("location: login_failed.php");
            exit();
            }
        }
     break;
    
case 4:
    //Check whether the query was successful or not
        if($result){      
    if(mysql_num_rows($result) == 1){
        //Login successful
            session_regenerate_id();
            $login = mysql_fetch_assoc($result);
            $_SESSION['SESS_username'] = $login['user_name'];
            $_SESSION['SESS_usercategory'] = $login['pass_word'];
            $_SESSION['SESS_password'] = $login['user_type'];
          
            session_write_close();
            header("location: upload4.php");
               exit();
            }else {
            //Login failed
            header("location: login_failed.php");
            exit();
            }
        }
       break;
     
case 5:
    //Check whether the query was successful or not
 if($result){      
          if(mysql_num_rows($result) == 1){
        //Login successful
            session_regenerate_id();
           $login = mysql_fetch_assoc($result);
            $_SESSION['SESS_username'] = $login['user_name'];
            $_SESSION['SESS_usercategory'] = $login['pass_word'];
            $_SESSION['SESS_password'] = $login['user_type'];
            session_write_close();
            header("location: upload5.php");
               exit();
            }else {
            //Login failed
            header("location: login_failed.php");
            exit();
            }
        }
     break;
default:die("Query failed");
     exit();
    }
       
?>

No comments:

Post a Comment