Jump to content
MakeWebGames

Floydian

Members
  • Posts

    900
  • Joined

  • Last visited

    Never

Everything posted by Floydian

  1. Re: Cant get my head around these? I can see that being in the realm of possibility. And it would bug the hell out of me if it happened on my site lol.
  2. Re: Cant get my head around these? I've personally seen css code that can stop SQL queries from working properly. I definitely don't understand it, but I could replicate it lol. I can see something breaking because of external stylesheets, but some sort of html/css hack that dupes items, seems very unlikely. Javascript could perhaps if and ONLY IF the source php code is itself bugged.
  3. Re: Gym error: Fatal error: Maximum execution time of 60 seconds exceeded ;) I'm glad that worked out!
  4. Re: Cant get my head around these? That'd be an interesting one to take a look at. I'm not sure I could do it without having some sort of access though. But there it is, I don't normally put that sort of free offer out there.
  5. Re: Gym error: Fatal error: Maximum execution time of 60 seconds exceeded for($i=0; $i<$_POST['amnt']; $i++) { $gain+=round(rand(1,3)/rand(800,1000)*rand(800,1000)*(($ir['will']+20)/150)); $ir['will']-=rand(1,3); if($ir['will'] < 0) { $ir['will']=0; } }   that's a loop my friend and if you loop through that a few million times, I could see it taking, oh, 10 minutes to complete perhaps
  6. Re: Gym error: Fatal error: Maximum execution time of 60 seconds exceeded Okay, so you're gym takes cash. If you open up the code, you will see that there is a loop in there. Now, I doubt that it's set on a one to one ratio. 15 million in cash = 15 million loops (that'd just be redic...) But that certainly sounds like what's happening. Please note that this "loop" has nothing to do with shops and whatnot. This loop is part of the code in the standard gym's people have.
  7. Re: Banning IP The description cpanel gives for this feature seems to directly contradict you there.  
  8. Re: Banning IP cpanel has a lil deal for banning ip's called: IP Deny Manager
  9. Re: Gym error: Fatal error: Maximum execution time of 60 seconds exceeded How many crystals is he using at a time? Each crystal = 1 loop. 10000 crystals = 10000 loops and that's a big shortcoming of the deal. That'd be the most likely problem.
  10. Re: Support Needed Why do I feel like I'm watching mystery science theater 3000?
  11. Re: Support Needed lol Spudinski, you're spot on with that. Somehow I doubt that dementor is capable of carrying out your advice as is evidenced by his complete lack of debugging skills. He can't even tell us which query error is in. Good luck with it bro ;) My advice is to study really hard.
  12. Re: MCC V1 Jail Problem Go into PHP MyAdmin and run that query. SELECT u.*,c.* FROM users u LEFT JOIN gangs c ON u.gang=c.gangID WHERE u.prison > 0 ORDER BY u.prison DESC   See what comes up there. If you see some sort of error, post back here with exactly what that error is.
  13. Re: IPN help I'll take that as a "no I haven't tested in the sandbox since the changes were made".   I think that's what you need to do. And then you can debug that thing to your heart's content ;)
  14. Re: IPN help And have you tested in the sandbox? I had to do extensive testing in the sandox in order to get mine to work. (Which by the way only uses one IPN script to credit an unlimited number of item types. Only limited by the number of donor item types in the database)   I used events to show me which parts of the file held the correct values in variables. For instance you can var_dump all of the post vars sent by pay pal so you can see just what is being sent. And it tells you if script execution made it to the point where the event add was placed.
  15. Re: Register.php create user on SMF Forum I nominate the phrase "stop reinventing the wheel" to be scheduled for a "reinventing"... hehehe   We've been using wheels for thousands of years. I think it's high time we came up with something better ^__^
  16. Floydian

    Security!

    Re: Security! $something = (int) $user_input; is basically the same as $something = intval($user_input); However the TC mentioned using abs(); Which just using inval() doesn't accomplish the same thing abs((int) $user_input); or abs(intval($user_input); does. ;) abs takes out any negative values. Personally, I prefer to use an if statement. If something is less than 0, tell the user this input was invalid. I know some people like to code their code to punish folks that try to cheat the game, but that also will punish folks that accidentally put in a negative. And it's not very professional to have a script do something unexpected.   for instance, if you are withdrawing -100 from your bank, you don't expect it to withdraw 100, you expect it to simply not allow it. And it I'd also expect an error message to accompany that as well. But if it simply withdraws 100, then I'd see it as a bit unprofessional. Does your bank do that? Would pay pal do that? Nope, and neither should you. ;)
  17. Re: Item Market Problem??? Most likely you have that function defined twice. function item_remove() {   something like that is probably in your script twice. Removing one of them will likely solve the problem. If that doesn't do it, there's one more thing that may be causing it. And as funny as this may sound, I've seen it happen. If you have your switch at the top of the script, before you define your functions, it's possible that error can happen. The simply solution to that is moving your functions before the rest of your code. But it's far more likely that it's the first problem that is the cause.
  18. Now we're going to look at adding a constructor and a destructor to our database class from PART 1 of this series. In the database class, we had the connection settings set in the class. This is very limiting because any time the class is used, you're always connected with the same user and to the same database. And we had to call the MyDatabase::Connect method before we connected to the database. Add to that the fact that nothing closed out the database connection either. The __construct() and __destruct() methods will perform these tasks for us exceptionally well. We'll take the Connect() method and change it's name to __construct() which will cause this function to run automatically. Then we'll add in four parameters to the __contruct(). This will allow the constructor to accept a username, password, host, and default database parameters. Here we go!   <?php class MyDatabase { // Define the class properties // Note how we can define all protected properties on one line in this example. protected $Connection, $QueryError, $ConnectionStatus = false; // Our constructor will connect to the database // It will store any errors encountered along the way in the $QueryError property. // The constructor doesn't return anything since we're creating a new object, you'd // be overwritting your object with anything that's returned. We'll use the ConnectionStatus // property to overcome this. function __construct($db_host, $db_user, $db_pass, $db_schema) { $this->Connection = mysql_connect($db_host, $db_user, $db_pass); if($this->Connection === false) { $this->QueryError = mysql_error(); $this->ConnectionStatus = false; } elseif (mysql_select_db($db_schema) === false) { $this->QueryError = mysql_error($this->Connection); $this->ConnectionStatus = null; } else { $this->ConnectionStatus = true; } } // This method is used to return to us the $QueryError property. function GetError() { return $this->QueryError; } // This function allows us to see and test if we're connected to the database. Additionally, // since the value of this property can be null, a null value tells us we're connected to the database, // but that we don't have a schema selected. function IsConnected() { return $this->ConnectionStatus; } // Our destructor closes out the connection and sets the ConnectionStatus property // to false. Since it's possible to call the destructor while the object still exists, // the ConnectionStatus property will allow us to know if the connection is live or not. function __destruct() { if ($this->ConnectionStatus !== false) { mysql_close($this->Connection); $this->ConnectionStatus = false; } } } $db = new MyDatabase('localhost', 'mememe', '1234abcd', 'my_game'); if ($db->IsConnected() !== true) { echo $db->GetError(); } else { echo "Successfully connected to the database."; } ?>   There's quite a bit different about this new Database class. At the end of this section of code where I instantiate a new MyDatabase object, note how I pass the connection settings when making the new object. Because of this, we have the ability to connect to as many databases as we want simultaneously. The __autoload() method automatically connects us to the the database. However, unlike the Connect() method from before, you can't return anything with a constructor. Instead of returning true or false upon success or failure, we add in a property called ConnectionStatus that will tell us if we're connected or not. Then we use the IsConnected() method to get that value and turn it into something usable outside of the class. I hope you're starting to see how classes are defined in a way that is similar to the internal functions of php. Consider the is_numeric, is_null, is this, is that family of functions. PHP internally has properties that tells itself what data type the data is. However, to access those properties, we must use functions. Your classes should follow this sort of methodology. I always ask myself, would the designers of PHP make functions that work this way? If the answer is no, then it's likely that my class method is not being designed the way it should be. Remember this: you don't need to know how serialize() works internally, and you don't need to know how substr() works internally. You simply use the function and it does what it's supposed to. If you had to open up the PHP source code which is written in C++ in order to figure out how to use a function, I think it'd be likely that you'd soon start getting tired of having to go to the trouble. Similarly, if you have to open up your class definition and look at how it's coded in order to see how to use it, then you're probably writing a class that is not all that good.   Lastly, this class defines a destructor that closes the connection for us, if there is one alive. We can accomplish this by putting in this line: unset($db); And your connection is closed. Easy? Maybe not at first, but once you get the hang of it, it can make coding go faster. Especially since something like a database class that's well written can be ported to any situation where database connectivity is needed :)
  19. Note that much of this article will only work with PHP 5. If you're still using PHP 4, I'd recommend that you upgrade and learn OOP in PHP 5 rather than learn the old ways of doing things that won't help you in the job market. There's a ton of information that could be included here, but my goal is to help the "uninitiated" become comfortable with OOP. OOP is "Object Oriented Programming". Click http://criminalexistence.com/ceforums/index.php?topic=22880.0 to see what OOP is and how it differs from Procedural Programming (what most of us PHP noobs use) Here is the basic syntax for creating a class:   <?php class MyClassName { } ?>   That is just the basic container. It's kinda like defining a function. class replaces function. Note that you do not use () after the class name. In keeping with OOP coding conventions (this is fairly standard across most OOP languages) we use "CamelCase" caps. This means that the first letter of each word is upper case, the rest is lower case, and there are no underscores. This helps seperate classes from functions and objects from variables. For our class to be useful, we need to add properties and methods. Properties are simply variables. Methods are basically functions. Let's look at an example of a simple MySQL database class. Try not to get bogged down in understanding what the code here does. I'm far more interested in introducing the patterns that you will see. Trust me, if you're new to OOP, you need to learn the patterns this type of coding follows before any of it will make sense to you. Remember, none of this is linear, it's all over the place, in a good way though ;)   <?php class MyDatabase { // Define the class properties private $Host = 'localhost'; private $UserName = 'mememe'; private $Password = '1234abcd'; private $SelectedDatabase = 'my_game'; private $Connection; private $QueryError; // This method will attempt to connect to the database and select a default schema (database) function Connect() { // Attempt to connect to the database. $this->Connection = mysql_connect($this->Host, $this->UserName, $this->Password); // If the connection attempt failed, we'll detect that here. if(!$this->Connection) { // Set an error message and return false so that we know this method failed to do what we wanted it to do. $this->QueryError = mysql_error(); return false; } // Attempt to select a database. We put this in an if so that we can also detect if this was succesfull // at the same time we selected a database if(!mysql_select_db($this->SelectedDatabase)) { // If we couldn't select a database, set an error message and return false so we know the method failed to // accomplish it's task $this->QueryError = mysql_error($this->Connection); return false; } else { // If we reached this point, we know we have an active database connection and a default schema selected. return true; } } // This method's only purpose is to return to us the error message stored in the private property: QueryError. function GetError() { return $this->QueryError; } } ?>   You've seen how to make the class container in the first example. Now we've pushed things considerably farther. After creating the container, we have declared some private properties. There are three basic options for properties. They can be public, protected, and private. Each of these has specialized uses that goes beyond the scope of this article. But here are the basics. Properties A public property is one that can be accessed from outside of the class. It can also be changed from outside of the class. A protected property is one that can only be accessed from within the class and can only be changed from inside the class. A private property is one that can only be accessed from within the class and can only be changed from inside the class. The difference between protected and private is that private properties are not inherited. Huh, what's inheritance? Well, let's leave that alone for now lol. At this point, I think it would be too confusing to inject a discussion of inheritance into the mix. Private and protected properties seperate the internal operation of your class from outside code far more than public properties. The general rule of thumb is that properties should not be accessed from outside of a class. For instance, you shouldn't ever have a need to access the MyDatabase::Connection property. It should only ever be used internally by the class. What is MyDatabase::Connection you ask? We'll talk briefly about the "scope resolution operator". :: is the scope resolution operater. :: is used to reference what class a property belongs to. MyDatabase::Connection is used as a way to reference a class name and a property name. I'll get back to the scope resolution a bit later. Methods Now we get to the first method. Methods are defined using the function key word. In fact, they operate much like functions do. Variables in your methods have a scope that is seperate from the global scope. If you aren't familiar with variable scope, may I suggest that you are biting off a bit more than you should be at this point. You really need to have a decent understanding of what variable scope is or class will be confusing. And it's confusing enough as it is without needing to add in more confusion. Methods are normally named with the CamelCase convention and without underscores, as are properties and class names. Methods can accept parameters to be passed to them, i.e., function blahblah($param1, $param2.....) {}. $this-> What is that $this you're seeing all over the place in the class definition? I'm hoping you asked that question as it means you have seen a pattern there. And this pattern is that $this gets used a lot in classes. It's extremely important to learn what $this does. So here it is: $this refers to your class! Uh, say what? I know it's confusing. Stay with me here. Let's visualize our class as an array, shall we? One note, this isn't a syntactically correct array. It's demonstrative of a class though and should help to put into our heads a visual image of the organization of a class.   <?php $MyDatabase = array( "Host" => 'localhost', "UserName" => 'mememe', "Password" => '1234abcd', "SelectedDatabase" => 'my_game', "Connection" => null, "QueryError" => null, "Connect" => function { $this->Connection = mysql_connect($this->Host, $this->UserName, $this->Password); if(!$this->Connection) { $this->QueryError = mysql_error(); return false; } if(!mysql_select_db($this->SelectedDatabase)) { $this->QueryError = mysql_error($this->Connection); return false; } else { return true; } }, "GetError" => { return $this->QueryError; } ) ?>   I want you to see how each property name and each method name is similar to a key in an array. In fact you can put an object into a foreach loop and access each public property! Methods, protected properties and private properties can't be accessed in this way. Within the class definition, $this has access to each one of those! $this->SelectedDatabase will hold the value of 'my_game'. $this->GetError(); will get the error message stored in the MyDatabase::QueryError property and return it. It's basically calling the GetError method! Using $this, you can access any part of a class from inside the class definition. $this->Connection = mysql_connect($this->Host, $this->UserName, $this->Password); On that line of code, we get the values stored in three of the properties and use them to create a connection to the database. Then our connection is stored in the MyDatabase::Connection property. **Random Thoughts** Instead of talking more about the scope resolution operator, I think it's better that I leave that for later. Classes have an entirely different structure than the code most of us are used to. I really want to emphasize that before you worry too much about what this database class does, I really want you to get used to the patterns that arise in classes. Specifically: a class definition starts with a list of properties, and then has methods. Methods make a lot of use of the $this variable. Keep those key points in mind, and classes will start to become a bit clearer. The last point I want to drill home is that I had to do a lot of consulting with the PHP manual to write all of these articles on OOP. I consider myself to still be a noob to OOP. And I get things wrong with it a lot. I've strived to make the information here as accurate as I can. And I've fact checked much of it, and have tested some of the code as well. Specifically, I've tested the database class and it does indeed connect to the database like it should. I'll leave you with an example of the database class that has some code after it that actually makes use of the class. All it will do is make a connection and then print out a response that tells you if you were connected successfully or not. ;)       <?php class MyDatabase { // Define the class properties private $Host = 'localhost'; private $UserName = 'mememe'; private $Password = '1234abcd'; private $SelectedDatabase = 'my_game'; private $Connection; private $QueryError; function Connect() { $this->Connection = mysql_connect($this->Host, $this->UserName, $this->Password); if(!$this->Connection) { $this->QueryError = mysql_error(); return false; } if(!mysql_select_db($this->SelectedDatabase)) { $this->QueryError = mysql_error($this->Connection); return false; } else { return true; } } function GetError() { return $this->QueryError; } } $db = new MyDatabase(); if (!$db->Connect()) { echo $db->GetError(); } else { echo "Successfully connected to the database."; } ?>
  20. This "How to" is for PHP >= 5. This will not work in PHP 4. We all know that each class definition should go in it's own file, and doing that forces us to use require(); or include(); so as to include the class definition of each class that we want to use. This can be a tedious process of managing scripts. As PHP has evolved and improved it's OOP implementation, they have introduced a way to eliminate the need for numerous includes. This is done by defining an __autoload(); function. Take note of the double underscore that begins the name. The PHP manual warns us to not use double underscores because any thing that has double underscores gets "magic" properties. I don't know specifically what those are, but they are documented in the manual if you want to look them up. Here is an example of an __autoload() function.   <?php // Class autoloader function __autoload($class_name) { require_once (BASE_URI . 'class/' . $class_name . '.php'); } ?>   BASE_URI is a constant that I define in my configuration file. I contains the absolute path to my public html folder. It would go something like this: /home/cpanel_username/public_html/ We use require_once because IF we make two objects that are of the same class, require_once will only load the file we need one time. Using require() would prevent us from being able to use two objects with the same class in one script. Suppose your class name was Database. You would need to put the class definition for Database into the file Database.php. That file would need to be stored in class/. /home/cpanel_username/public_html/class/Database.php Then when you instantiate the class: $db = new Database(); Database is inserted here: /home/cpanel_username/public_html/class/ >>>> Database <<<< .php It's that simple. :)
  21. Re: Register.php create user on SMF Forum I'm by no means an expert on this, but I do believe mdshare has spoken about this before. If I recall what he said correctly, he recommends using the smf registration and login for registering new users and for logging into the game. Your users table would then be the smf users table. This of course would mean that any place in mccodes where the users table is used, you would have to edit in the appropriate table name. Possibly smf_users. And then you would have to edit in the appropriate column names. You may be able to put in column names that are the same names that mccodes uses. This approach would allow one login to login your users to both the game and the forums. If you have a seperate users table for both, and if you do not modify the login system, then your game and your forum will require seperate logins. I'm sure this isn't exhaustive information of the subject, but it should suffice to say that it requires a lot of work to make it seamless.
  22. Re: [OOP] __construct() ???? You're welcome! I have to admit that being PHP 5 person, as in I learned to code using PHP 5, I did make a mistake with what I thought about PHP 4. There is a way to simulate a constructor in PHP 4. The correction is noted in the main post.
  23. What is a __construct()? Introduced into PHP 5 and it's vastly upgraded OOP implemenation are constructors and destructors. Every time an object is created, the __construct() method is called. Every time an object is closed out by either being unset($object); or when the script execution is finished, the __destruct() method is called. When creating a new object, arguments can be passed to the constructor. This can be a big benefit. We'll use a Database class to show how to pass arguments to a constructor.   <?php $db = new Database(DB_USER, DB_PASS, DB_HOST, DB_DATABASE); ?>   That's it. What we've done here is we have four constants defined. These contain our database's user name, password, host, and the database we will be using. If you are going to be using multiple databases, you can make a different object for each one. $db1, $db2, and so on. The benefit of the constructor in this case is that connecting to the database is automatically handled when the object is created. The destructor will automatically close the database for us. To close the database connection we use this: unset($db); And the connection is closed. Since it's assumed we'll not be querying the database after closing the connection, we no longer need the object either, so we put those all in one. the __destruct() method does not accept any arguments. The lack of constructors and destructors in PHP 4 is a seriously limiting aspect of PHP 4's implementation of OOP. EDIT: A constructor can be simulated in PHP 4. I'll describe how to do this at the end of this post. I don't know of any way to simulate a destructor however. You can call the __construct() and __destruct() methods at any time if you choose to do so. $db->__construct(); $db->__destruct(); Calling the destructor does not close out the object. The destructor is simply intended to be code that is executed when an object is closed out, not as a way to close out an object. I use a class in my game for controlling many aspects of users. The constructor method does things like checking to see if the player is online or not. It also does a basic query gathering information from the users table like the username, their cash on hand, and information needed for drawing user bars. There's much more that the class can do after that, but that's basically what the constructor does. A page on my game would start with something like:   <?php // I'm not passing anything to the constructor because the only information it needs // is the user id of the player which is stored in a session, and since sessions // are super globals, the $_SESSION['userid'] variable is available to the constructor as is. $control = new Control(); if (!$control->isLoggedIn()) { header(Location: blah blah); // redirect people that aren't logged in to the login page. } $energy = $control->userBar('energy'); // get a user bar that is drawn up for me automatically. $nerve = $control->userBar('nerve '); $brave = $control->userBar('brave '); $my_name = $control->myName(); echo <<<EOT $my_name</p> $energy</p> $nerve</p> $brave</p> EOT; ?>   Hopefully this demonstrates that the constructor handles the pre processing of quite a bit of data. Every other method I've called, uses information stored in properties that the __contruct function initializes and then returns that data in a meaningful format. I've had to pass not even one variable to these methods. The userBar() method needs to know what kind of bar we're dealing with in case a bar as special formatting, like energy going up to 150% instead of 100%, or in the case of nerve, it's not a % at all, but a litteral number. And then the name of that bar is generated to match the bar, i.e., Nerve: 22, Energy: 125%. When you look at my header code, you'll see that it's crazy how simple it looks. And it's because I've put all the mechanics into a class, and all of the presentation after it. Enjoy ;)   Simulating a constructor in PHP 4: What you do is make a function that has the same name as your class. That function will automatically be called when the class is instantiated.   <?php class MyExample { function MyExample($something) { echo $something; } } $something = "This is how you can simulate a constructor in PHP 4!"; $an_example = new MyExample($something); ?>
  24. Re: [FAQ] Quotes, and heredoc That is a library. It's short and small. But it's a library. lol I use the YUI library and it's JSON implementation which is also written by Douglas Crockford. It's actually a better implemenation of the JSON methods than his standalone one. No reason to do ajax without a library? I'm not sure what that has to do with quotes, but so be it. It's your opinion. Mine is, there is a reason to do just that. I also see reasons to use an ajax library. I think my concepts here are a bit more "open minded" than yours, no? Be a bit more open minded. Not everyone will want to code like you do Deception ;) So quotes? Any thoughts on quotes?
  25. Have you ever tried to do a google search for information on "class" or "php class", "php classes" and the like? It's completely useless unless you're looking to find a teacher, and a class they are doing. It took me a long time to figure out what a php class is and what it does. I'm not intending to write a guide on how classes work, but merely to introduce some ideas of what a class is in php for those, who like me, may have googled "php class" and have come up empty handed. The first thing you should know, is the names of two types of programming styles.   Procedural Programming Object Oriented Programming   Procedural Programming For all of you that are familiar with mccodes, procedural programming is what you are seeing, with the one exception being the the Header class in versions 1 and 2, and the Database class in version 2. Procedural is simply doing things in a linear fashion. You do one thing, then another, and then another. The code follows a straight progression from start to end. There's nothing wrong with this style of coding.   Object Oriented Programming OOP, short for object oriented programming is where classes and objects come into play. What is a class? A class is a definition. It's that simple. Making a new class is defining how something ought to work. If I am going to make a database class, I'm defining how connecting to, get data from, and submitting data to a database should work. Creating the class does nothing to the database by itself. To use a comparison from the author Larry Ullman, a class is like a blueprint for a house. That blueprint is not in and of itself a house, and that blueprint doesn't build a house. It tells you how to build it. What is an object? An object is what you have when you use a class. To illustrate, let's say we have defined a class for handling database connections. We might call that class "Database". In order to use that class, we'll use the code below.   <?php $db = new Database(); ?>   What we have done here, is used the class called "Database" and created an object called $db. There's a lot more I could get into here, but the point of this writing is to simply introduce the concept of classes and what the basic nomenclature is. You know know that a class is a definition of how something works. An object is how you put that class definition into use. From here, numerous books have been written about classes and objects. Hopefully if classes and objects confused you before, as to what they are in their most basic form, this sets some boundary definitions for each. **Random Thoughts** Just an FYI, the Header class in mccodes is a terrible implementation of a class. Why is that? What is the class designed to do? It's designed to wrap a php page in an html header and footer. There's nothing wrong with wrapping your pages in a header and footer template, but using a class to do so is not the way to go. The first thing I ask myself is what do we gain by using a class for the header? Nothing. Since we have gained nothing, then why did we do it? Is it just because we could? That's not a good reason to use OOP. The functions in the header class could just as easily have been functions in the global_func.php file and nothing about mccodes would work differently. The Database class in mccodes vs 2 is a different case. It makes perfect sense to use a database class to simplify using a database with php. mccodes Database class suffers from some lack of functionality, which is based in part on PHP 4's deficient OOP implementation. However, beginning with PHP 5, OOP in PHP is far far better does make things work more efficiently and makes it easier to code. Now there's no reason to rip out the header class and go the procedural route since the header class does work. But keep it in mind when you are making a class of your own the differences between the header and the database class, and you can use them as base examples of a bad and a good implementation of a class.
×
×
  • Create New...