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 - <form method="post" action="check_login.php">
-
<table><tr><td>Name: <input type="text" name="Username" size="10" maxlength="40" /></td></tr>
-
<tr><td>Password: <input type="password" name="Password" size="10" maxlength="10" /></td></tr>
-
<tr><td><input type="submit" name="Submit" value="login" /></td></tr></table>
-
-
-
</form>
In my controller class(check_login.php) -
class controller{
-
private $username;
-
private $password;
-
function_construct();
-
{
-
$this->username=$_POST[Username];
-
$this->password=$_POST[Password];
-
-
require_once(../model/Loginsystem.php)
-
$loginSystem = new LoginSystem();
-
if($loginSystem->doLogin($this->username,$this->password))
-
}
-
}
model class - class LoginSystem
-
{
-
var $db_host,
-
$db_name,
-
$db_user,
-
$db_password,
-
$connection,
-
$username,
-
$password;
-
-
/**
-
* Constructor
-
*/
-
function LoginSystem()
-
{
-
require_once('settings.php');
-
-
$this->db_host = $dbhost;
-
$this->db_name = $dbname;
-
$this->db_user = $dbuser;
-
$this->db_password = $dbpassword;
-
}
-
-
-
-
/**
-
* Check username and password against DB
-
*
-
* @return true/false
-
*/
-
function doLogin($username, $password)
-
{
-
$this->connect();
-
-
$this->username = $username;
-
$this->password = $password;
-
-
// check db for user and pass here.
-
$sql = sprintf("SELECT * FROM `admin` WHERE user = '$this->username' and Pass = '$this->password'",
-
$this->clean($this->username), md5($this->clean($this->password)));
-
-
$result = mysql_query($sql, $this->connection);
-
-
// If no user/password combo exists return false
-
if(mysql_affected_rows($this->connection) != 1)
-
{
-
$this->disconnect();
-
return false;
-
}
-
else // matching login ok
-
{
-
$row = mysql_fetch_assoc($result);
-
-
// more secure to regenerate a new id.
-
session_regenerate_id();
-
-
//set session vars up
-
-
-
}
-
-
$this->disconnect();
-
return true;
-
}
-
-
/**
-
* Destroy session data/Logout.
-
*/
-
function logout()
-
{
-
unset($_SESSION['LoggedIn']);
-
unset($_SESSION['userName']);
-
session_destroy();
-
}
-
-
/**
-
* Connect to the Database
-
*
-
* @return true/false
-
*/
-
function connect()
-
{
-
$this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL");
-
-
mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!");
-
-
// Valid connection object? everything ok?
-
if($this->connection)
-
{
-
return true;
-
}
-
else return false;
-
}
-
-
/**
-
* Disconnect from the db
-
*/
-
function disconnect()
-
{
-
mysql_close($this->connection);
-
}
That means that you are calling session_start too many times. Try to only call it once.
33 14222
That's generally the way OOP works... What is the problem?
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.
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 -
-
-
-
<?php session_start();
-
-
require_once('../controller/check_login.php');
-
-
if(isset($_POST['Submit']))
-
{
-
if((!$_POST['Username']) || (!$_POST['Password']))
-
{
-
// display error message
-
header('location: main_login.php?msg=1');// show error
-
exit;
-
}
-
-
$loginSystem = new CheckLogin();
-
if($loginSystem->check($_POST['Username'],$_POST['Password']))
-
{
-
/**
-
* Redirect here to your secure page
-
*/
-
header('location: index.php');
-
}
-
else
-
{
-
header('location: main_login.php?msg=2');
-
exit;
-
}
-
}
-
-
/**
-
* show Error messages
-
*
-
*/
-
function showMessage()
-
{
-
if(is_numeric($_GET['msg']))
-
{
-
switch($_GET['msg'])
-
{
-
case 1: echo "Please fill both fields.";
-
break;
-
-
case 2: echo "Incorrect Login Details";
-
break;
-
}
-
}
-
}
-
?>
-
-
-
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
-
<table><tr><td>Name: <input type="text" name="Username" size="10" maxlength="40" /></td></tr>
-
<tr><td>Password: <input type="password" name="Password" size="10" maxlength="10" /></td></tr>
-
<tr><td><input type="submit" name="Submit" value="login" /></td></tr></table>
-
-
-
</form>
-
-
</div>
-
-
-
<div><p> </p></div></div></div>
-
-
-
</body>
-
</html>
-
-
-
-
-
-
Call from controller to model in line no 23 -
<?php session_start();
-
require_once('../model/LoginSystem.class.php');
-
class ChechLogin
-
{
-
var $username,
-
$pass;
-
-
function isLoggedIn()
-
{
-
if($_SESSION['LoggedIn'])
-
{
-
return true;
-
}
-
else return false;
-
}
-
-
function check($user , $pass)
-
{
-
$this->username=$user;
-
$this->pass=$pass;
-
-
$loginSystem = new LoginSystem();
-
if($loginSystem->doLogin($this->username,$this->pass))
-
{
-
session_regenerate_id();
-
-
//set session vars up
-
$_SESSION['LoggedIn'] = true;
-
$_SESSION['userName'] = $this->username;
-
-
}
-
return true;
-
}
-
-
function logout()
-
{
-
unset($_SESSION['LoggedIn']);
-
unset($_SESSION['userName']);
-
session_destroy();
-
}
-
-
}
-
?>
-
-
<?php session_start();
-
-
-
-
class LoginSystem
-
{
-
var $db_host,
-
$db_name,
-
$db_user,
-
$db_password,
-
$connection,
-
$username,
-
$password;
-
-
/**
-
* Constructor
-
*/
-
function LoginSystem()
-
{
-
require_once('settings.php');
-
-
$this->db_host = $dbhost;
-
$this->db_name = $dbname;
-
$this->db_user = $dbuser;
-
$this->db_password = $dbpassword;
-
}
-
-
-
-
/**
-
* Check username and password against DB
-
*
-
* @return true/false
-
*/
-
function doLogin($username, $password)
-
{
-
$this->connect();
-
-
$this->username = $username;
-
$this->password = $password;
-
-
// check db for user and pass here.
-
$sql = sprintf("SELECT * FROM `admin` WHERE user = '$this->username' and Pass = '$this->password'",
-
$this->clean($this->username), md5($this->clean($this->password)));
-
-
$result = mysql_query($sql, $this->connection);
-
-
// If no user/password combo exists return false
-
if(mysql_affected_rows($this->connection) != 1)
-
{
-
$this->disconnect();
-
return false;
-
}
-
else // matching login ok
-
{
-
$row = mysql_fetch_assoc($result);
-
-
// more secure to regenerate a new id.
-
//session_regenerate_id();
-
-
//set session vars up
-
//$_SESSION['LoggedIn'] = true;
-
// $_SESSION['userName'] = $this->username;
-
-
}
-
-
$this->disconnect();
-
return true;
-
}
-
-
-
function connect()
-
{
-
$this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Unable to connect to MySQL");
-
-
mysql_select_db($this->db_name, $this->connection) or die("Unable to select DB!");
-
-
// Valid connection object? everything ok?
-
if($this->connection)
-
{
-
return true;
-
}
-
else return false;
-
}
-
-
/**
-
* Disconnect from the db
-
*/
-
function disconnect()
-
{
-
mysql_close($this->connection);
-
}
-
-
/**
-
* Cleans a string for input into a MySQL Database.
-
* Gets rid of unwanted characters/SQL injection etc.
-
*
-
* @return string
-
*/
-
function clean($str)
-
{
-
// Only remove slashes if it's already been slashed by PHP
-
if(get_magic_quotes_gpc())
-
{
-
$str = stripslashes($str);
-
}
-
// Let MySQL remove nasty characters.
-
$str = mysql_real_escape_string($str);
-
-
return $str;
-
}
-
-
-
-
-
}
-
-
?>
-
where is problem in my code could you please help me....!
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.
it redirect me to main_login.php and i want to redirect to index.php (line 23 in first tag)
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.
- Firstly, the Location header should be capitalized.
- Secondly, you named your class "ChechLogin" but you try to create an object called "CheckLogin".
- Thirdly, "ChechLogin::check()" will always return true.
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.
the error on index.php is
Warning: require(../model/check_login.php): failed to open stream: No such file or directory in /var/www/test/view/makeSecure.php on line 14 Call Stack: 0.0005 330052 1. {main}() /var/www/test/view/index.php:0 0.0706 333796 2. require('/var/www/test/view/makeSecure.php') /var/www/test/view/index.php:6 Fatal error: require(): Failed opening required '../model/check_login.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/test/view/makeSecure.php on line 14 Call Stack: 0.0005 330052 1. {main}() /var/www/test/view/index.php:0 0.0706 333796 2. require('/var/www/test/view/makeSecure.php') /var/www/test/view/index.php:6
So, as you can see, it's not finding the files that you are looking for. Check the paths.
Thanks kovik it works thanks you very much the path was wrong in make secure now it works but the following error comes still.
otice: A session had already been started - ignoring session_start() in /var/www/test/controller/check_login.php on line 1 Call Stack: 0.0004 329732 1. {main}() /var/www/test/view/index.php:0 0.0007 333480 2. require('/var/www/test/view/makeSecure.php') /var/www/test/view/index.php:6 0.0015 344924 3. require('/var/www/test/controller/check_login.php') /var/www/test/view/makeSecure.php:14 0.0015 344968 4. session_start() /var/www/test/controller/check_login.php:1 Notice: A session had already been started - ignoring session_start() in /var/www/test/model/LoginSystem.class.php on line 1 Call Stack: 0.0004 329732 1. {main}() /var/www/test/view/index.php:0 0.0007 333480 2. require('/var/www/test/view/makeSecure.php') /var/www/test/view/index.php:6 0.0015 344924 3. require('/var/www/test/controller/check_login.php') /var/www/test/view/makeSecure.php:14 0.0028 376756 4. require_once('/var/www/test/model/LoginSystem.class.php') /var/www/test/controller/check_login.php:2 0.0028 376800 5. session_start() /var/www/test/model/LoginSystem.class.php:1
That means that you are calling session_start too many times. Try to only call it once.
Ooh thats great...!
It works fine Thanks kovik thanks very much if you are on my back i will be the php master soon.
hope you will help me in future.
ok kovik if i want to retrieve the data in html table from admin e.g
ID NAME PASSWORD
1 abc *******
then how can i call this from view to controller and from controller to model remember that the select query should be in model class and data will be displayed on html page in view class.
The way that MVC works is that your Controller decides which Model to use and which View to use, based on user input. On the internet, "user input" is generally simplified down to the selected URL.
The Controller chooses the model which has the data it needs, and it choses the View that displays that data the way it needs to be displayed. Then, it injects the data into the view and outputs the result.
You'd want a Model class that creates an array of data from the database data, and a View class that uses that data to make your HTML table.
i do this in my html - $rs=new SelectSubject();
-
-
$select=$rs->get();
-
-
//$select=mysql_query("select * from subject") or die(mysql_error());
-
-
-
-
while($rs=mysql_fetch_array($select))
-
{
then in controller like this - class SelectSubject
-
{
-
function get()
-
{
-
$selectsubject=new LoginSystem();
-
-
$selectsubject->getSubject();
-
}
-
and in model - function getSubject()
-
{
-
$this->connect();
-
$sql=sprintf("select * from subject") or die(mysql_error());
-
$result=mysql_query($sql);
-
return $result;
-
}
this give me the error - Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /var/www/test/view/add_subject.php on line 112 Call Stack: 0.0007 338948 1. {main}() /var/www/test/view/add_subject.php:0 0.0215 397500 2. mysql_fetch_array() /var/www/test/view/add_subject.php:112
code of line 112 is - while($rs=mysql_fetch_array($select))
That just means that $select is not a resource. You can verify this by echoing get_type(). If it's not a result, then you are probably getting a MySQL error. However, you don't handle it correctly.
This: - $sql=sprintf("select * from subject") or die(mysql_error());
-
$result=mysql_query($sql);
Should be this: - $sql=sprintf("select * from subject");
-
$result=mysql_query($sql)or die(mysql_error());
Also, the call to sprintf() is unnecessary.
kovik i do the changes but still not working is there any other possibility to call from to controller and then model but the data should be displayed in view and query should be in model.
What errors are you getting?
there is no error but the data is not displayed in html table.
insert query is working fine but select does't work
i change the code but still not working plz help whats wrong with this code.
Notice: Undefined variable: dbhost in /var/www/test/model/LoginSystem.class.php on line 31 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php:0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php:105 0.1268 403536 3. LoginSystem->LoginSystem() /var/www/test/controller/check_login.php:75 Notice: Undefined variable: dbname in /var/www/test/model/LoginSystem.class.php on line 32 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php:0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php:105 0.1268 403536 3. LoginSystem->LoginSystem() /var/www/test/controller/check_login.php:75 Notice: Undefined variable: dbuser in /var/www/test/model/LoginSystem.class.php on line 33 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php:0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php:105 0.1268 403536 3. LoginSystem->LoginSystem() /var/www/test/controller/check_login.php:75 Notice: Undefined variable: dbpassword in /var/www/test/model/LoginSystem.class.php on line 34 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php:0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php:105 0.1268 403536 3. LoginSystem->LoginSystem() /var/www/test/controller/check_login.php:75 Warning: mysql_connect(): Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/test/model/LoginSystem.class.php on line 91 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php:0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php:105 0.1287 403536 3. LoginSystem->getSubject() /var/www/test/controller/check_login.php:77 0.1287 403536 4. LoginSystem->connect() /var/www/test/model/LoginSystem.class.php:180 0.1287 403712 5. mysql_connect() /var/www/test/model/LoginSystem.class.php:91 Unable to connect to MySQL
3 days ago it gives me the error of not valid resource and now its give me the above error
The error says "Undefined variable: dbhost". The only time that you reference "$dbhost" is in your constructor for the LoginSystem class. It is never defined. Did you mean to include it in your constructor's declaration?
Instead of: - function LoginSystem()
-
{
-
require_once('settings.php');
-
-
$this->db_host = $dbhost;
-
$this->db_name = $dbname;
-
$this->db_user = $dbuser;
-
$this->db_password = $dbpassword;
-
}
Try: - function LoginSystem($dbhost, $dbname, $dbuser, $dbpassword)
-
{
-
require_once('settings.php');
-
-
$this->db_host = $dbhost;
-
$this->db_name = $dbname;
-
$this->db_user = $dbuser;
-
$this->db_password = $dbpassword;
-
}
And be sure to send this variables when creating instances of the class. If those are supposed to be defined in "settings.php", then I'd say that this is a fairly bad design, but eh.
ya my setting.php contain "localhost","user","password","db_name" i comment that and give variable values in loginsystem class.
now
Unable to connect to MySQL
i changed the constructor like this - function LoginSystem()
-
{
-
//require_once('settings.php');
-
-
$this->db_host ="localhost"; //$dbhost;
-
$this->db_name = "kims"; //$dbname;
-
$this->db_user = "root"; //$dbuser;
-
$this->db_password = "root"; //$dbpassword;
-
}
-
and it gives me no error but still data is not displayed in html table i thing i return something wrong in my controller and model.
Have you tried connecting directly using those credentials? They are likely incorrect if you can't connect.
ya its correct the user is login successfully and the data can be inserted into the table but the only problem is to retrieve that data from database table.
If your error is "Unable to connect to MySQL", then that means that your connection is not being made. Unless you have a different error, that's the only explanation. Check here for more options.
hi all....i want to ask that you hav included setting.php under function loginSystem....but you never created file setting.php
Atli 5,058
Expert 4TB
It's not shown there, but it obviously exists. You generally don't include all your code when asking questions on forums, just what is relevant.
Sign in to post your reply or Sign up for a free account.
Similar topics
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. :
...
|
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...
|
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,...
|
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
|
by: jaz |
last post by:
plzz help me....how to giv login security using sql
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |