473,695 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

login class using MVC approach

semanticnotion
66 New Member
i want to make a login class using MVC approach. First my html form code is in my view folder then i want authorizing code in controller class and the query and the connection are under my model class so
is it possible to call the method of one class into another class e.g (line no 11 in second tag correspond to line no 31 in third tag)

html form
Expand|Select|Wrap|Line Numbers
  1. <form method="post" action="check_login.php">
  2. <table><tr><td>Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="Username" size="10" maxlength="40"  /></td></tr>
  3. <tr><td>Password:&nbsp; <input type="password"  name="Password" size="10" maxlength="10"  /></td></tr>
  4. <tr><td><input type="submit" name="Submit" value="login" /></td></tr></table>
  5.  
  6.  
  7. </form>

In my controller class(check_log in.php)

Expand|Select|Wrap|Line Numbers
  1. class controller{
  2. private $username;
  3. private $password;
  4. function_construct();
  5. {
  6. $this->username=$_POST[Username];
  7. $this->password=$_POST[Password];
  8.  
  9. require_once(../model/Loginsystem.php)
  10. $loginSystem = new LoginSystem();
  11. if($loginSystem->doLogin($this->username,$this->password))
  12. }
  13. }

model class
Expand|Select|Wrap|Line Numbers
  1. class LoginSystem
  2. {
  3.     var    $db_host,
  4.         $db_name,
  5.         $db_user,
  6.         $db_password,
  7.         $connection,
  8.         $username,
  9.         $password;
  10.  
  11.     /**
  12.      * Constructor
  13.      */
  14.     function LoginSystem()
  15.     {
  16.         require_once('settings.php');
  17.  
  18.         $this->db_host = $dbhost;
  19.         $this->db_name = $dbname;
  20.         $this->db_user = $dbuser;
  21.         $this->db_password = $dbpassword;
  22.     }
  23.  
  24.  
  25.  
  26.     /**
  27.      * Check username and password against DB
  28.      *
  29.      * @return true/false
  30.      */
  31.     function doLogin($username, $password)
  32.     {
  33.         $this->connect();
  34.  
  35.         $this->username = $username;
  36.         $this->password = $password;
  37.  
  38.         // check db for user and pass here.
  39.         $sql = sprintf("SELECT * FROM `admin` WHERE user = '$this->username' and Pass = '$this->password'",
  40.         $this->clean($this->username), md5($this->clean($this->password)));
  41.  
  42.         $result = mysql_query($sql, $this->connection);
  43.  
  44.         // If no user/password combo exists return false
  45.         if(mysql_affected_rows($this->connection) != 1)
  46.         {
  47.             $this->disconnect();
  48.             return false;
  49.         }
  50.         else // matching login ok
  51.         {
  52.             $row = mysql_fetch_assoc($result);
  53.  
  54.             // more secure to regenerate a new id.
  55.             session_regenerate_id();
  56.  
  57.             //set session vars up
  58.  
  59.  
  60.         }
  61.  
  62.         $this->disconnect();
  63.         return true;
  64.     }
  65.  
  66.     /**
  67.      * Destroy session data/Logout.
  68.      */
  69.     function logout()
  70.     {
  71.         unset($_SESSION['LoggedIn']);
  72.         unset($_SESSION['userName']);
  73.         session_destroy();
  74.     }
  75.  
  76.     /**
  77.      * Connect to the Database
  78.      * 
  79.      * @return true/false
  80.      */
  81.     function connect()
  82.     {
  83.         $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL");
  84.  
  85.         mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!");
  86.  
  87.         // Valid connection object? everything ok?
  88.         if($this->connection)
  89.         {
  90.             return true;
  91.         }
  92.         else return false;
  93.     }
  94.  
  95.     /**
  96.      * Disconnect from the db
  97.      */
  98.     function disconnect()
  99.     {
  100.         mysql_close($this->connection);
  101.     }
Oct 15 '10 #1
33 14406
kovik
1,044 Recognized Expert Top Contributor
That's generally the way OOP works... What is the problem?
Oct 15 '10 #2
semanticnotion
66 New Member
It dos't work.
Oct 18 '10 #3
kovik
1,044 Recognized Expert Top Contributor
Thank you for your detailed and insightful debugging process. I'm sure the fact that it "dos't work" is completed unrelated with the fact that you NEVER SET YOUR SESSION VARIABLES.

Could just be me, though.
Oct 18 '10 #4
semanticnotion
66 New Member
ok i change the code just little now i just want to call from view to controller and then from controller to model the code is bellow but still not working.

Call from view to control in line no 18
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. <?php session_start();
  5.  
  6.     require_once('../controller/check_login.php');
  7.  
  8.     if(isset($_POST['Submit']))
  9.     {
  10.         if((!$_POST['Username']) || (!$_POST['Password']))
  11.         {
  12.             // display error message
  13.             header('location: main_login.php?msg=1');// show error
  14.             exit;
  15.         }
  16.  
  17.         $loginSystem = new CheckLogin();
  18.         if($loginSystem->check($_POST['Username'],$_POST['Password']))
  19.         {
  20.             /**
  21.              * Redirect here to your secure page
  22.              */
  23.             header('location: index.php');
  24.         }
  25.         else
  26.         {
  27.             header('location: main_login.php?msg=2');
  28.             exit;
  29.         }
  30.     }
  31.  
  32.     /**
  33.      * show Error messages
  34.      *
  35.      */
  36.     function showMessage()
  37.     {
  38.         if(is_numeric($_GET['msg']))
  39.         {
  40.             switch($_GET['msg'])
  41.             {
  42.                 case 1: echo "Please fill both fields.";
  43.                 break;
  44.  
  45.                 case 2: echo "Incorrect Login Details";
  46.                 break;
  47.             }
  48.         }
  49.     }
  50. ?>
  51.  
  52.  
  53. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  54. <table><tr><td>Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="Username" size="10" maxlength="40"  /></td></tr>
  55. <tr><td>Password:&nbsp; <input type="password"  name="Password" size="10" maxlength="10"  /></td></tr>
  56. <tr><td><input type="submit" name="Submit" value="login" /></td></tr></table>
  57.  
  58.  
  59. </form>
  60.  
  61.         </div>
  62.  
  63.  
  64.     <div><p>&nbsp;</p></div></div></div> 
  65.  
  66.  
  67. </body>
  68. </html>
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  

Call from controller to model in line no 23


Expand|Select|Wrap|Line Numbers
  1. <?php session_start();
  2. require_once('../model/LoginSystem.class.php');
  3.     class ChechLogin
  4.         {
  5.         var $username,
  6.             $pass;
  7.  
  8.         function isLoggedIn()
  9.     {
  10.         if($_SESSION['LoggedIn'])
  11.         {
  12.             return true;
  13.         }
  14.         else return false;
  15.     }
  16.  
  17.             function check($user , $pass)
  18.             {
  19.                 $this->username=$user;
  20.                 $this->pass=$pass;
  21.  
  22.                 $loginSystem = new LoginSystem();
  23.         if($loginSystem->doLogin($this->username,$this->pass))
  24.                 {
  25.                     session_regenerate_id();
  26.  
  27.             //set session vars up
  28.             $_SESSION['LoggedIn'] = true;
  29.                     $_SESSION['userName'] = $this->username;
  30.  
  31.                 }
  32.                 return true;
  33.             }
  34.  
  35. function logout()
  36.     {
  37.         unset($_SESSION['LoggedIn']);
  38.         unset($_SESSION['userName']);
  39.         session_destroy();
  40.     }
  41.  
  42.     }
  43. ?>
  44.  
Expand|Select|Wrap|Line Numbers
  1. <?php session_start();
  2.  
  3.  
  4.  
  5. class LoginSystem
  6. {
  7.     var    $db_host,
  8.         $db_name,
  9.         $db_user,
  10.         $db_password,
  11.         $connection,
  12.         $username,
  13.         $password;
  14.  
  15.     /**
  16.      * Constructor
  17.      */
  18.     function LoginSystem()
  19.     {
  20.         require_once('settings.php');
  21.  
  22.         $this->db_host = $dbhost;
  23.         $this->db_name = $dbname;
  24.         $this->db_user = $dbuser;
  25.         $this->db_password = $dbpassword;
  26.     }
  27.  
  28.  
  29.  
  30.     /**
  31.      * Check username and password against DB
  32.      *
  33.      * @return true/false
  34.      */
  35.     function doLogin($username, $password)
  36.     {
  37.         $this->connect();
  38.  
  39.         $this->username = $username;
  40.         $this->password = $password;
  41.  
  42.         // check db for user and pass here.
  43.         $sql = sprintf("SELECT * FROM `admin` WHERE user = '$this->username' and Pass = '$this->password'",
  44.         $this->clean($this->username), md5($this->clean($this->password)));
  45.  
  46.         $result = mysql_query($sql, $this->connection);
  47.  
  48.         // If no user/password combo exists return false
  49.         if(mysql_affected_rows($this->connection) != 1)
  50.         {
  51.             $this->disconnect();
  52.             return false;
  53.         }
  54.         else // matching login ok
  55.         {
  56.             $row = mysql_fetch_assoc($result);
  57.  
  58.             // more secure to regenerate a new id.
  59.             //session_regenerate_id();
  60.  
  61.             //set session vars up
  62.             //$_SESSION['LoggedIn'] = true;
  63.                    // $_SESSION['userName'] = $this->username;
  64.  
  65.         }
  66.  
  67.         $this->disconnect();
  68.         return true;
  69.     }
  70.  
  71.  
  72.     function connect()
  73.     {
  74.         $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL");
  75.  
  76.         mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!");
  77.  
  78.         // Valid connection object? everything ok?
  79.         if($this->connection)
  80.         {
  81.             return true;
  82.         }
  83.         else return false;
  84.     }
  85.  
  86.     /**
  87.      * Disconnect from the db
  88.      */
  89.     function disconnect()
  90.     {
  91.         mysql_close($this->connection);
  92.     }
  93.  
  94.     /**
  95.      * Cleans a string for input into a MySQL Database.
  96.      * Gets rid of unwanted characters/SQL injection etc.
  97.      * 
  98.      * @return string
  99.      */
  100.     function clean($str)
  101.     {
  102.         // Only remove slashes if it's already been slashed by PHP
  103.         if(get_magic_quotes_gpc())
  104.         {
  105.             $str = stripslashes($str);
  106.         }
  107.         // Let MySQL remove nasty characters.
  108.         $str = mysql_real_escape_string($str);
  109.  
  110.         return $str;
  111.     }
  112.  
  113.  
  114.  
  115.  
  116. }
  117.  
  118. ?>
  119.  

where is problem in my code could you please help me....!
Oct 19 '10 #5
kovik
1,044 Recognized Expert Top Contributor
Once again, you haven't told us what the errors are... We're not going to write your code for you. Tell us where it's going wrong and what it should do.
Oct 19 '10 #6
semanticnotion
66 New Member
it redirect me to main_login.php and i want to redirect to index.php (line 23 in first tag)
Oct 19 '10 #7
semanticnotion
66 New Member
and if i remove the controller layer and put the code of check_login.php in logsystem(model ) then it works fine but i want it to pass through controller layer.
Oct 19 '10 #8
kovik
1,044 Recognized Expert Top Contributor
  1. Firstly, the Location header should be capitalized.
  2. Secondly, you named your class "ChechLogin " but you try to create an object called "CheckLogin ".
  3. Thirdly, "ChechLogin::ch eck()" will always return true.
Oct 19 '10 #9
semanticnotion
66 New Member
Thanks for your deep attention sir i correct the spelling of check and redirect me to index.php but nothing is displayed on that page.
Oct 19 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2141
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : HttpContext.Current.Response.Cookies.Add("myNewCookie"); I am wondering if there are any landmines that I should know about, or if this will work pretty much as I am expecting a cookie should.
3
4095
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts the Properties into Fields when it comes out of the Web Service). I have this at the moment: Private _aDataSource As Object 'Person class
2
4235
by: Sasquatch | last post by:
I'm having trouble creating a simple login page using the asp:login control. I followed some instructions in a WROX book, "Beginning ASP.NET 2.0," and the instructions are very straight forward, but it won't work for me. Here's what I did. 1. Created a new folder named "testlogin" 2. Turned that folder into an application using the IIS properties 3. Created two new web form pages, /testlogin/default.aspx and /testlogin/login.aspx...
3
3056
by: mudgilgaurav | last post by:
I am very keen to learn AJAX and want to use it in my website Can anyone give me a bunch of code abt ajax and as well as tell me the flow of code
1
1391
by: jaz | last post by:
plzz help me....how to giv login security using sql
3
6686
by: Ghanathe | last post by:
Hello!!! Can any one tell how to retrieve user name and other details like first name,address,contact no. and so on.... and display in welcome page after login page using c# and sql server plzzzzzzzzzzzz Thanking u Regards, Deepshika
4
2304
by: John Devlon | last post by:
Hi, I'm trying to create a login page using the default login controle. I also would like to use a custom Ms Acces database instead of a sql server database. Can anyone please tell me how to or where I can find some usefull information ? Thanx
16
16650
by: GVNPublic123 | last post by:
Hello, What I am trying to do, is login YouTube using C# (HTTPWebRequest). The problem is, I don't really know how to do it. Is there any URL I could fill using the username and pass and therefore login, or do I have to somehow fill the form manually and than click on login? I tried catching the login URL, but this is all I got: GET...
1
4454
yawar
by: yawar | last post by:
Hi, I am a newbie in System.Net programming though I have worked on mshtml but I want to do things more perfectly and with speed. I have created one software that login to gmail using httpwebrequest and check that email ID is active or deleted. But, that was I think simple login process. For that I had first tried with Wireshark but may be its too complicated or I am new to wireshark, anyways I was not able to find out that where is...
0
8577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9120
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8996
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8853
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4345
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2277
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.