TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-05-2007, 01:49 PM   #1 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default Help with classes

I just followed some tutorials about classes in PHP and I made this:
PHP Code:
<?php

/**
 * @author Michael
 * @copyright 2007
 */
define("USER""test");
define("NAME""Michael");
define("PASS""mypass");

class 
MySQL {
    public function 
connect($db_user,$db_pass,$db_name) {
        
mysql_connect("localhost"$db_user$db_pass) or die(mysql_error());
        
mysql_select_db($db_name) or die(mysql_error());
    }
}
$connect = new MySQL();
$connect->connect(USER,PASS,NAME);

?>
Can someone tell me what I did wrong?
Swordbeta is offline  
Reply With Quote
Old 12-05-2007, 01:59 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Seems fine, what error does it give?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-05-2007, 02:02 PM   #3 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default

Hm...nvm,strange enough it works now! :D
Maybe the server had some delay or something.
Swordbeta is offline  
Reply With Quote
Old 12-05-2007, 04:00 PM   #4 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

A bit of constructive criticism, if you don't mind. I understand that you're just starting out with classes and objects.

When you design a class, it should be functionally independent and fluid. You should consider not hard-coding anything like the server address / name into the class, and you should definitely never allow a class to kill a script or interact with the script outside itself.

A better way might be something more like this:
PHP Code:
class MySQL {

    
// variable to store an error message, or FALSE 
    
private $error FALSE;

    
// constructor - remember this cannot return a value!!
    
function __construct($db_host,$db_user,$db_pass,$db_name) {
        
$this->connect($db_host,$db_user,$db_pass);
        
$this->new_db($db_name);
    }

    
// connection method
    
public function connect($host,$user,$pass) {
        
mysql_connect($host,$user,$pass) OR $this->error mysql_error();
    }
    
// db selection method
    
public function new_db($db) {
        
mysql_select_db($db) OR $this->error mysql_error();
    }

    
// error handler
    
public function error_handler() {
        
// assumes no error has occurred
        
if ( $this->error == FALSE ) {
            return 
FALSE;
        } else {
            return 
$this->error;
        }
    }

So in the example class design, you have an error handler method that might be called from the script to check the status. Let the script determine what to do at that point. You might simply want it to log the error or send you an email.

Hope that helps, let me know if you have any questions.
SOCK is offline  
Reply With Quote
The Following User Says Thank You to SOCK For This Useful Post:
Wildhoney (12-05-2007)
Old 12-05-2007, 05:08 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I understand what you mean, SOCKS, but if that were the case and you're not to hard-code anything into your classes, surely class constants would be a thing of the past? Are you saying that they are now unnecessary?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-05-2007, 05:50 PM   #6 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I understand what you mean, SOCKS, but if that were the case and you're not to hard-code anything into your classes, surely class constants would be a thing of the past? Are you saying that they are now unnecessary?
No, class constants have their place. But creating a class where the host, user, password or other such data are hard-coded misses the point. The class should be portable and be flexible enough to be used anywhere.
SOCK is offline  
Reply With Quote
Old 12-05-2007, 06:30 PM   #7 (permalink)
The Wanderer
Newcomer 
 
Swordbeta's Avatar
 
Join Date: Dec 2007
Location: Holland
Posts: 18
Thanks: 0
Swordbeta is on a distinguished road
Default

thanks for explaining sock :)
I got another problem...
It's something totally different,I'm making a file editor but problems with the login.
The pass and username are in the config.php,but when entering a wrong password it logs me in =/
PHP Code:
<?php

/**
 * @author Michael
 * @copyright 2007
 */
session_start();
include(
"config.php");
echo 
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
 <html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Phatom Panel</title>
<style>
BODY { font-size: 11px; color: #7d7b7b;background-color:#1F1F1F; }
a:link, a:visited, a:active { text-decoration: none; color: #B9B9B9 }
a:hover { color: #D7D7D7; text-decoration:underline }
.table { border:1px solid #181818;text-align: center;background-color: #262626; }
</style>
</head>
<body>"
;
if(isset(
$_SESSION['pp_admin'])){
    if(
$_SESSION['pp_admin']!=$pass){
        unset(
$_SESSION['pp_admin']); 
    }
}
if(!isset(
$_GET['act'])){
    if(!isset(
$_SESSION['pp_admin'])){
    echo 
"<center>
    <table width='500' class='table'><tr>
    <td><h2><b>Login</b></h2></td>
    <td><form action='ppanel.php?act=admin' method='post'><p><br /><b>Admin Name:</b> <input type='text' name='name' /></p><p><b>Password:</b> <input type='password' name='pass' /></p><p><input type='submit' value='Login'></p></form>
    </td></tr></table></center>"
;
    }else if(isset(
$_SESSION['pp_admin'])){
        echo 
"<center><table width='500' class='table'><tr>
    <td><b>You're already logged in!</b><br /><a href='ppanel.php?act=panel'>Go to the Phantom Panel</a></td></tr></table></center>"
;
    }
}
if(
$_GET['act']=="admin"){
    
$a 0;
    if(isset(
$_POST['name']) && $_POST['name']!="" && isset($_POST['pass']) && $_POST['pass']!=""){
    if(
md5($_POST['pass'])==$pass){
        
$a 0;
    }else{
        
$a 1;
    }
    if(
$_POST['name']==$username){
        
$a 0;
    }else{
        
$a 1;
    }}
    echo 
"<center><table width='500' class='table'><tr><td>";
    if(
$a==0){
        
$_SESSION['pp_admin'] = $pass
        if(isset(
$_SESSION['pp_admin'])){
        echo 
"<b>Login Succesful!</b><br /><a href='ppanel.php?act=panel'>Go to the Phantom Panel</a>";
        }else{
        echo 
"<b>Unknown error.</b>";
        }
        }else{
                echo 
"<b>Admin Name/Password didn't matched.</b>";
        }
    echo 
"</center></td></tr></table>";
}
if(
$_GET['act']=="panel"){
        echo 
"<center><table width='500' class='table'><tr><td>";
    if(isset(
$_SESSION['pp_admin']) && $_SESSION['pp_admin']==$pass){
        echo 
"<font size='1'><a href='ppanel.php?act=logout'>Logout</a></font><h2>Welcome to your Phantom Panel!</h2><br /><br /><br /><img src='pp_images/edit.png' alt='' /> <a href='ppanel.php?act=edit&path=/'>File Editor</a><br /><br /><img src='pp_images/key.png' alt='' /><a href='ppanel.php?act=pass'>Change password</a>";
        }else{
        echo 
"<a href='ppanel.php'><h2>Please Login</h2></a>";    
        }
        echo 
"</td></tr></table>";
}
if(
$_GET['act']=="logout"){
    if(isset(
$_SESSION['pp_admin'])){
        unset(
$_SESSION['pp_admin']);
        echo 
"<center><table width='500' class='table'><tr><td><h2>Your now logged out</h2></td></tr></table</center>";
    }else{
        echo 
"<center><table width='500' class='table'><tr><td><a href='ppanel.php'><h2>Please login first.</h2></a></td></tr></table</center>";
    }
}
if(
$_GET['act']=="pass"){
        if(isset(
$_SESSION['pp_admin']) && $_SESSION['pp_admin']==$pass){
    if(!
$_POST){
    echo 
"<center><table width='500' class='table'><tr><td><h2>Change your password</h2><br /><br /><form action='ppanel.php?act=pass' method='post'><p><b>Current Password:</b> <input type='password' name='pass' /></p><p><b>New Password:</b> <input type='password' name='pass1' /></p><p><b>Confirm new password:</b><input type='password' name='pass2'></p><p><input type='submit' value='Change password'></form></td></tr></table</center>";
    }else if(
$_POST){
        if(
$_POST['pass'] && $_POST['pass1'] && $_POST['pass2']){
            if(
md5($_POST['pass'])==$pass && $_POST['pass1']==$_POST['pass2']){
                
$f "config.php";
                
$fh fopen($f'w') or die("can't open file");
                
$data "
                <"
."?php 
                \$username = \""
.$username."\";
                \$pass = \""
.md5($_POST['pass2'])."\"; 
                ?"
.">";
                
fwrite($fh$data);
            }else{
                echo 
"<center><table width='500' class='table'><tr><td><h2>Passwords didn't matched!</h2></td></tr></table</center>";
            }
        }else{
            echo 
"<center><table width='500' class='table'><tr><td><h2>Please fill in all fields!</h2></td></tr></table</center>";
        }
    }
    }else{
        echo 
"<center><table width='500' class='table'><tr><td><a href='ppanel.php'><h2>Please login!</h2></a></td></tr></table</center>";
    }
}
echo 
"<br /><br /><br /><br /><center>Phantom Panel copyright Michael of Phantom-designs.net</center></body></html>";
?>
Swordbeta is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:41 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design