geudrik Posted November 20, 2010 Share Posted November 20, 2010 Basically, I'm still trying to wrap my head around OOP. What I'm trying to do here is a simple OOP user login script. But when I submit the form, all that happens is that the text fields reset them selves and nothing that I feel should be happening, happens. ie: I submit login data, and either it displays an error or reirects to index page. Neither happen, the form merely resets. Where am I going wrong? <form name="loginform" id="loginform" action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <p> <label>Username<br> <input name="user" id="user_login" class="input" size="20" tabindex="10" type="text" /> </label> </p> <p> <label>Password<br> <input name="pass" id="user_pass" class="input" value="" size="20" tabindex="20" type="password"></label> </p> <p class="forgetmenot"><label><input name="rememberme" id="rememberme" value="forever" tabindex="90" type="checkbox"> Remember Me</label></p> <p class="submit"> <input name="login" id="submit" class="button-primary" value="Log In" tabindex="100" type="submit"> <input name="redirect_to" value="/users.php" type="hidden"> </p> </form> <?php if(isset($_POST['login'])) { $username = $_POST['user']; $password = $_POST['pass']; include("./classes/class.users.php"); USERS::login($username, $password); } ?> <?php // Yes, my DATABAASE::DoIT(1) // (0) is working as intended (from a different include file) class USERS { var $user; var $pass; var $email; ////////////////////////////////////////////////////////////////////////////////////////////// function login($user, $pass) { include("/var/www/config.php"); DATABASE::DoIt('1'); $hashword = sha1($CONFIG['salt1']."$pass".$CONFIG['salt2']); $sql = "SElECT * FROM users WHERE username='$user' AND hashword='$hashword'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1) { while ($row = mysql_fetch_assoc($result)) { define('USERS_AUTHENTICATED', true); $_SESSION['USERS_username'] = $row['username']; $_SESSION['USERS_userid'] = $row['userid']; DATABASE::DoIt('0'); header("Location: index.php"); } } else { $_SESSION['loginError'] = true; DATABASE::DoIt('0'); return $_SESSION['loginError']; } DATABASE::DoIt('0'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/ Share on other sites More sharing options...
trq Posted November 20, 2010 Share Posted November 20, 2010 This... USERS::login($username, $password); Should be.... $user = new USERS; $user->login($username, $password); For starters. Your login() function also uses a while loop that is required. Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137099 Share on other sites More sharing options...
geudrik Posted November 20, 2010 Author Share Posted November 20, 2010 Your login() function also uses a while loop that is required. I don't understand what you mean by this. The while loop is there to populate the session vars, or should I be doing it a different way? I've changed the call as per your suggestion, but still having the same issues. :s As a side note: I have not overlooked string cleanup - I just have not yet implemented it. It's on the to-do list though! Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137100 Share on other sites More sharing options...
trq Posted November 20, 2010 Share Posted November 20, 2010 I don't understand what you mean by this. The while loop is there to populate the session vars Your only logging in 1 user, you do not need a loop to get 1 result. I've changed the call as per your suggestion, but still having the same issues. :s I would assume your DATABASE object's doit() method isn't static either. We need to see more code. Your might want to take a look at how classes actually work in php, you seem quite off track. Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137111 Share on other sites More sharing options...
geudrik Posted November 20, 2010 Author Share Posted November 20, 2010 My database class is static, and looks like the following. As per your remark, Thorpe Your might want to take a look at how classes actually work in php' date=' you seem quite off track.[/quote'] That's why I'm posting here. I've read a good many tutorials on OOP, and the book that I have is "PHP and MySQL Web Development" by Welling, Thomas, 3rd Ed. Can you tell me / Explain what I appear to be missing/not understanding, fundamentally speaking, in terms of the way that OOP works? Like I said, I've very much new to OOP, so any help you can provide would be much appreciated Since I'm only trying to pull one row / authenticate one user, I should instead do something more like: <?php // We won't use a WHILE loop, as we're only pulling one row... $info = mysql_fetch_array($result); define('USERS_AUTHENTICATED', true); $_SESSION['USERS_username'] = $info['username']; $_SESSION['USERS_userid'] = $info['userid']; DATABASE::DoIt('0'); header("Location: index.php"); ?> DATABASE class <?php class DATABASE { function DoIt($option) { if ($option = 'yes' || 'connect' || '1' || 'open') { mysql_connect( 'localhost', 'fuzzyUser', 'SuperS3cr3t*' ) or die("shit broke on the database connection"); mysql_select_db('media') or die("Selecting the database failed. Pat fails."); } else if ($option = 'no' || 'close' || '0' || 'disconnect') { mysql_close(); } else { die('unknown database error'); } } // Close easy DB Functionality } // Close our DATABASE class ?> Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137159 Share on other sites More sharing options...
KevinM1 Posted November 20, 2010 Share Posted November 20, 2010 Like Thorpe said, you're calling your methods wrong. :: is the scope resolution operator. When used in the manner you're using, it means one is trying to invoke a static method. Neither of your methods are static. Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137178 Share on other sites More sharing options...
geudrik Posted November 20, 2010 Author Share Posted November 20, 2010 Like Thorpe said, you're calling your methods wrong. :: is the scope resolution operator. When used in the manner you're using, it means one is trying to invoke a static method. Neither of your methods are static. Am I missing a fundamental definition of what static means? /me scratches head The DATABASE class, as far as I would have though, is/was static - simply because it really doesn't do anything other than invoke a database connection/kill it. Given that neither are static, I should be calling my DATABASE connection as... <? $connect = new DATABASE; $connect->DoIt('1'); ?> Just like I'm calling my Login code currently.. correct? <? include("./classes/class.users.php"); $user = new USERS; $user->login($username, $password); ?> Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137182 Share on other sites More sharing options...
KevinM1 Posted November 20, 2010 Share Posted November 20, 2010 Like Thorpe said, you're calling your methods wrong. :: is the scope resolution operator. When used in the manner you're using, it means one is trying to invoke a static method. Neither of your methods are static. Am I missing a fundamental definition of what static means? Yes, you are: http://php.net/manual/en/language.oop5.static.php Quote Link to comment https://forums.phpfreaks.com/topic/219273-oop-basic-user-login-unsure-where-im-going-wrong/#findComment-1137221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.