Hey guys, not so long ago I posted this
and I got a lot of good answers and help from the members of codecall. I have been busy at work so i have not got to program to much lately. working on it a little at a time I finaly finished my first class that i can use on my projects. I am going to post the code (which is incomplete but most of it is done) below. I would appreciate it if you could look at it and tell me if I am doing the class Design correctly. This is the thing i miss most about school, having someone to look at my work and tell me if I am doing it correctly. I won't post just to have people check my work often but I think it is absolutly esential to make sure I am doing this corectly as it will effect just about every language I ever use.Ok so i am about to start codeing a website and i decided to do it using OOP. I understand HOW to program in oop but i don't understand how to design in oop. For example lets say i want to make a simple guest book with a reply center like here on code call, what is the object? Is it the guest book altogether, is it say the reply center one and the posts another, is it portions of the reply center being multiple objects? I am confused on how to aproach this. I know my Java book explains how to design a dog object and i understand that but when you get to things a little more complex than a dog i don't know how to break it apart. can some one explain it to me?
Heres the code:
ThanksCode:class Login
{
//Set size of input and password fileds
var $fieldSize;
//Set max charicters allowed in user field
var $userLength = 50;
//Set max characters allowed in password field
var $passLength = 50;
//Holds value user entered for UserName
var $user;
//Holds value user entered for Password
var $pass;
function __construct($userLength, $passLength, $fieldSize)
{
$this->userLength = $userLength;
$this->passLength = $passLength;
$this->fieldSize = $fieldSize;
}
function displayLogin()
{
//Check to see if page is postback
if(!empty($_POST['user']))
{
//If postback run checkLogin, pass in paramaters from login form
$this->checkLogin($_POST['user'], $_POST['pass']);
}
//Display login Form
echo "<form action='$self' method='Post'>";
echo "User Name: <br /><input type='text' name='user' size='" . $this->fieldSize . "' maxlength='" . $this->userLength . "' /><br />";
echo "Password: <br /> <input type='password' name='pass' size='" . $this->fieldSize . "' maxlength='" . $this->passLength . "' /><br />";
echo "<input type='submit' value='Login' />";
echo "</form>";
}
function checkLogin($user, $pass)
{
//Remove tags and negate special charicters to prevent an ejection attack. Trim off whitespace.
$this->user = strip_tags(trim(mysql_real_escape_string(strtolower($user))));
$this->pass = strip_tags(trim(mysql_real_escape_string($pass)));
//check to make sure user and password were both filled out
if(!empty($this->user) && !empty($this->pass))
{
//Check to see if username and password exsist
$loginQuery = "Select * from login where u_name='" . $this->user . "' AND u_pass='" . $this->pass . "'";
$runLoginQuery = mysql_query($loginQuery)or die("An Error has Occured. Please copy the bellow error code and send it to an administrator <br /><br /> Error Code:<br />" . mysql_error());
//get results of query
$getLogin = mysql_fetch_array($runLoginQuery);
//If a value was returned than username and password matched so set the session
if(!empty($getLogin['u_name']))
{
$this->setSession($this->user);
}
else
{
echo "You Fail";
}
}
else
{
//Display an error mesage
echo "<b><u>Login failed!</u></b>";
}
}
function setSession($user)
{
echo "Yay, your loged in";
$_SESSION['u_name'] = $user;
echo "<script>location.href='" . $self . "'</script>";
}
function logOut()
{
//Destroy session
session_destroy();
}
}
~Zero
Not correct. You are mixing logic with display and web programming doesn't work like that, in a large application it results in confusion.
I would do the following for the user class:
Code:<?php
/*
FILE: user_session.php
*/
class user_session
{
public static function register_user($username, $password, $remember = false)
{ // check if user exists and then register username, id, etc on $_SESSION, beware of possible SQL injection on parameters
/* ... */
$_SESSION['user_session'] = true;
}
public static function unregister_user()
{ // you don't necessarially needs to destroy the session, but remove variables associated with user
unset($_SESSION['user_username']);
/* ... */
}
public static function get_username()
{ // return username for e.g.
// return username if the user_session has been registered
return (isset($_SESSION['user_session']) ? $_SESSION['user_username'] : false);
/* OR */
// you can also try this approach and throw an exception, i would prefer this way because it would be easier to detect an error outside this class
if (isset($_SESSION['user_session']))
return ($_SESSION['user_username']);
else
throw new exception('user has not been registered');
}
public static function get_userid()
{ // may be very usefull in a web application
/* more or less the same as get_username() */
}
public static function get_userlevel()
{ // if you have user-like permissions, this may be needed for e.g.
/* more or less the same as get_username() */
}
public static function is_registered()
{ // checks if user is registered in session, if yes it means that he is logged
return (isset($_SESSION['user_session']));
}
}
?>
And for the presentation, i would do the following (if not using a template engine):
Code:<?php
/*
FILE: index.php
*/
require_once 'user_session.php';
if (isset($_POST['user_login']))
user_session::register_user($_POST['username'], $_POST['password']);
else if (isset($_POST['user_logout']))
user_session::unregister_user();
?>
</html>
<head>
<title>my application</title>
</head>
<body>
<?php include 'loginbox_obj.php'; ?>
</body>
</html>
Code:<?php
/*
FILE: loginbox_obj.php
*/
require_once 'user_session.php';
<?php if (user_session::is_registered()): ?>
<table>
<tr>
<td><label>Welcome, <?php echo user_session::get_username(); ?></label></td>
</tr>
<tr>
<td><input type="submit" name="user_logout" value="Logout" /></td>
</tr>
</table>
<?php else: ?>
<table>
<tr>
<td><label>Username: </label></td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td><label>Password: </label></td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td><input type="submit" name="user_login" value="Login" /></td>
</tr>
</table>
<?php endif ?>
?>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks