Jump to content

Strider64

Members
  • Posts

    473
  • Joined

  • Last visited

  • Days Won

    12

Everything posted by Strider64

  1. /* Get the path and filename that you are currently on. */ $phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL); $path_parts = pathinfo($phpSelf); $basename = $path_parts['basename']; This is one way of making PHP_SELF safe and to utilize it all you would do something like this: <form class="formStyle" action="<?php echo $basename; ?>" method="post"> However, if you really just want to be safe then just do this: <form class="formStyle" action="register.php" method="post">
  2. There also a lot of neat things you can do with a classes, such as this for example: Let's say you have a class called Member.php <?php # Member class - Store user info and functions to access/control the flow of data. class Member { // The member attributes (variables) containing required and optional information. // The attributes must correspond to the database table columns: private $id = NULL; private $userType = NULL; // Required private $username = NULL; // Required private $email = NULL; // Required private $pass = NULL; // Required private $fullName = NULL; private $validation_code = NULL; private $address = NULL; private $city = NULL; private $state = NULL; private $zipCode = NULL; // Method returns the user ID: public function getId() { return $this->id; } // Grab the user's username: public function getUsername() { return $this->username; } // Grab the user's full name: public function getFullName() { return $this->fullName; } // Grab the password: public function getPass() { return $this->pass; } public function getUserType() { return $this->userType; } // Clear the password once user is logged in: public function clearPass() { $this->pass = NULL; } public function getEmail() { return $this->email; } // Method returns a Boolean if the user is an administrator: public function isAdmin() { return ($this->userType == 'author'); } public function isSysop() { return ($this->userType == 'sysop'); } public function isNewUser() { return ($this->userType == 'public'); } // Method returns a Boolean indicating if the user is an administrator // or if the user is the original author of the provided page: public function canEditPage(Page $page) { return (($this->isAdmin() || ($this->id == $page->getCreatorId()))); } // Method returns a Boolean indicating if the user is an administrator or an author: public function canCreatePage() { return ($this->isAdmin() || $this->isSysop()); } } The you can simply do the following to put the pull a corresponding record from a database table: // Check against the database: $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username'; $stmt = $pdo->prepare($query); $stmt->execute(array(':username' => $_POST['username'])); /* The following fetches the class Member and creates an instance $stored_user_data */ $stmt->setFetchMode(PDO::FETCH_CLASS, 'Member'); $stored_user_data = $stmt->fetch(); Then for example you can check the user's input against the database table password like this: // Verify Stored Hashed Password against input: if ($stored_user_data) { $result = password_verify($_POST['pass'], $stored_user_data->getPass()); } However, you got to learn how to crawl before you walk, so following php_bad_boy's post would probably be the prudent thing to do.
  3. JavaScript will do that and there are already some nice validation libraries out there on the web.
  4. What I do is check one check at the database constraint and one at registration time with a validation check (no Ajax). Here's the method I use for the validation check: public function checkAvailability($username) { $db = Database::getInstance(); $pdo = $db->getConnection(); $this->query = "SELECT 1 FROM users WHERE username=:username"; $this->query_params = [':username' => $username]; $this->stmt = $pdo->prepare($this->query); $this->stmt->execute($this->query_params); $this->result = $this->stmt->fetch(); if ($this->result) { return 'Username is invalid or not available, please re-enter!'; } } If the validation doesn't get it, when the user's data is submitted and by some chance there is another user with the same name then it will catch it as Jacques1 has already. The only bummer is the user will have to re-enter all his/her data, but I can't see that happening unless it's a really busy website (A good thing ) or a freak happening.
  5. A good place to start -> http://php.net/manual/en/pdo.prepared-statements.php
  6. You should treat the sidebar as a navigational menu and the sidebar as a container: For example: <div id="sidebar"> <h3>Popular Articles</h3> <div id="sidebarNav"> <ul> <li><a href="http://pickmysmoker.com/masterbuilt_propane.php/">Masterbuilt Propane Smoker Review</a></li> <li><a href="#">Continue on...</a></li> <!-- Continue on with nav menu --> </ul> </div><!--/ End of #sidebarNav --> </div><!--/ End of #sidebar --> I would also check into using clearfix (Do a Google Search to find good examples) and utilizing box-sizing: border-box; for your sidebar issues. Do it this way you will be able to really stylize your Sidebar Navigation. As a personal preference I prefer to have my sidebar navigation on the left rather than the right, but like I said it's a personal preference.
  7. There is a good book called "PHP Objects, Patterns, and Practice" by Matt Zandstra (Fourth Edition) that is pretty good on OOP and more. I would consider doing iCRUD interface for it forces you to structure your OOP. <?php /* The iCrud interface. * The interface identifies four methods: * - create() * - read() * - update() * - delete() */ interface iCrud { public function create($data); public function read(); public function update($data); public function delete($id=NULL); } Besides then you can swap your classes if you move on to a different project. I know books can be tedious, but the only true way I found how to learn the material is either via books or by taking a college course on the subject. Just my .02 cents.
  8. Simply save the path and name of file to a database table, along a text description of the image (which you would get from the user's input). Then when you can retrieve all the necessary information to do what you want it to do. That's the thought process that I would do, I have done a picture gallery without the description portion, but it wouldn't be to hard to add that feature. For all I would have to do is add another column in my database structure. Anyways, throw in some JQuery (JavaScript) into the mix and you can really get some neat results. This is an example of what I do after I process my file upload then I proceed to save the info to a database table: // Insert into the database using PDO and prepared statements: $query = 'INSERT INTO pictures (thumbnail, picName, subDirectory, user_name) VALUES (:thumbnail, :picName, :subDirectory, :userName)'; $stmt = $pdo->prepare($query); $result = $stmt->execute(array(':thumbnail' => $thumbImage, ':picName' => $image, ':subDirectory' => $name, ':userName' => 'Strider64')); $resultDir = displayDir($pdo, $name); // Re-display the thumbnails:
  9. A variable doesn't have to be in a DOM element to read it from AJAX, you can simply read it in from php via Ajax or by pulling the data in indirectly from a DOM element (In you case it would probably be a form element input?) using a keyboard press (I believe Google's search engine does something like that).
  10. All three scripts are different, which makes it very confusing and your question doesn't relate to the code provided (Well at least I don't think it does) ??? You're missing the value attribute in you <option> tag: <form action="process.php" method="post"> Game: <select name="game"> <option>Select a Game</option> <option value="Counter-Strike 1.3">Counter-Strike 1.3</option> <option value="Counter-Striker: Source">Counter-Strike: Source</option> <option value="Counter-Strike: GO">Counter-Strike: GO</option> <option value="ArmA: 3">ArmA: 3</option> <option value="Call of Duty: 4">Call Of Duty: 4</option> </select> Slots <select name="game"> <option>Desired Slots</option> <?php for ( $x = 10; $x <= 30 ; $x++ ) { echo '<option value="' . $x . '">' . $x . '</option>' . "\n"; } ?> </select> <input type="submit" name="submit" value="Submit" /> </form>
  11. He skipped the question on the test, probably couldn't answer it. Probably just trying to see what the answer was that was on the test.
  12. From a book I read by Larry Ullman that MVC isn't really a true "pattern" (He said it, I didn't so I don't want people coming down on me ), so I guess the Enterprise Pattern could be MVC though I haven't gotten that far in another book that I'm reading about OOP. But I'm sure some guru here will answer this question better.
  13. If you just want to check to see if it is a valid integer : <?php /* If you just want to check to see if it's a valid integer */ if (isset($_POST['id']) && !filter_var($_POST['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) { echo "I'm not an integer<br>"; } elseif (isset($_POST['id'])) { echo 'The id is ' . $_POST['id'] . '<br>'; } ?> <form action="" method="post"> Enter Number <input type="text" name="id" > <input type="submit" name="submit" value="Submit"> </form> An maybe this will clear up the confusion : http://us.php.net/manual/en/intro.filter.php $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
  14. No, you have to convert correctly over to mysqli, a simple way to convert would just go over to php.net and look at the examples -> http://php.net/manual/en/mysqli.construct.php
  15. In your php.ini there is upload_max_filesize = 8000M ;8GB that determines how large of a file that can be uploaded.
  16. Thanks, this helps me wrap my head around it better.
  17. I am seriously trying to wrap my head around abstract classes, but the more I try to understand it the more I get confused. I've even dug out a book by Larry Ullman to no avail. Here's the example in the book: Shape abstract class <?php # Script 6.1 - Shape.php /* This page defines the Shape abstract class. * The class contains no attributes. * The class contains to abstract methods. * - getArea() * - getPerimeter() */ abstract class Shape { // No Attributes to declare. // No constructor or destructor defined here. // Method to calculate and return the area. abstract protected function getArea(); // Method to calculate and return the perimeter. abstract protected function getPerimeter(); } // End of Shape Class. Triangle class: <?php # Script 6.2 - Triangle.php /* This page defines the Triangle class. * The class contains two attributes: * - private $_sides (array) * - private $_perimeter (number) * The class contains three methods. * - __construct() * - getArea() * - getPerimeter() */ class Triangle extends Shape { // Declare the attributes: private $_sides = array(); private $_perimeter = NULL; // Constructor: function __construct($s0 = 0, $s1 = 0, $s2 = 0) { // Store the values in the array: $this->_sides[] = $s0; $this->_sides[] = $s1; $this->_sides[] = $s2; // Calculate the perimeter: $this->_perimeter = array_sum($this->_sides); } // End of constructor. // Method to calculate and return the area: public function getArea() { // Calculate and return the area: return (SQRT( ($this->_perimeter/2) * (($this->_perimeter/2) - $this->_sides[0]) * (($this->_perimeter/2) - $this->_sides[1]) * (($this->_perimeter/2) - $this->_sides[2]) )); } // End of getArea() method. // Method to return the perimeter: public function getPerimeter() { return $this->_perimeter; } // End of getPerimeter() method. } // End of Triangle Class. and to execute in finding the area and perimeter of a triangle: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Abstract Class</title> </head> <body> <?php # Script 6.3 - abstract.php // This page uses the Triangle class - (Script 6.2), which is derived from Shape (Script 6.1) // load the class definitions: require('Shape.php'); require('Triangle.php'); // Set the triangle's sides: $side1 = 5; $side2 = 10; $side3 = 13; // Print a little introduction: echo "<h2>With sides of $side1, $side2, and $side3...</h2>\n"; // Create a new triangle: $t = new Triangle($side1, $side2, $side3); // Print the area. echo '<p>The area of the triangle is ' . $t->getArea() . '</p>'; // Print the perimeter. echo '<p>The perimeter of the triangle is ' . $t->getPerimeter() . '</p>'; // Delete the object: unset($t); ?> </body> </html> What is the point of having an abstract class when it runs fine without it being declared? For example I can take out extends Shape in the Triangle class and it will work just fine? I don't know if If I will ever understand this concept of abstract and interface classes.... Any help in this matter will be greatly appreciated. Thanks John OK, I fooled around with it some more and took out the include('Shape.php') and it spat an out an error stating it wasn't found. So is an abstract class just forcing a person to adhere to the methods being declared in the abstract class? (I'm probably not explaining this right).
  18. Personally I would call $return something else, for that is close to using a reserve word that's just me: // In your JQuery $("#sub").click(function() { var name = $("#name").val(); var town = $("#town").val(); jQuery.ajax({ type: "POST", url: "postScripts/addDetail.php", data: 'name='+name+'&town='+town, success: function(info) { var name = info.name, town = info.town; /* Continue with code */ } }); return false })
  19. You were close. btw I don't know what you mean it won't open up in the browser for it should had even with an error(s). /* You had this */ $message = "Well done! It took you '$_POST["counter"]' tries!"; /* It should had been something like this */ $message = 'Well done! It took you ' . $_POST["counter"] . ' tries!';
  20. The call method would know what the attribute(variable) is doing because you would have to set it. Here's a little script that I put together using all magic methods (I personally would probably never use all magic methods, but that is just me): <?php class Phone { private $number = array(); public function __set($name, $value) { $this->number[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->number)) { return $this->number[$name]; } } public function __call($method, $arg) { if (array_key_exists($method, $this->number)) { return "Thank You for calling " . $this->number[$method] . "<br>"; } } } $myNumber = new Phone; $myNumber->phoneNumber = '555-421-4685'; // Set the attribute: echo $myNumber->phoneNumber; // Get the attribute: echo '<br>'; echo $myNumber->phoneNumber(); // Call the method: echo '<br>'; $myNumber->telephone = '555-555-1313'; // Set the attribute: echo $myNumber->telephone; // Get the attribute: echo '<br>'; echo $myNumber->telephone(); // Call the method:
  21. It an utilities file that I have on each page I have this // Check for a user in the session: $user = (isset($_SESSION["user"])) ? $_SESSION["user"] : NULL; Of course when you login you would put the user in session, then I simply do this where I have my login/logout (Logout is simply taking the user out of session) If ($user) { /* Logout Script */ } else { /* Login Script */ }
  22. OOPS Though I did create a script that grabs the timezone using PHP, Ajax and JQuery : <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>TimeZone</title> </head> <body> <p>Today's Date and Time is <span id="result"></span></p> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="jstz.min.js"></script> <script> $(document).ready(function() { var timezone = jstz.determine(), myTimezone = timezone.name(), // Grab my Timezone: params = {timezone: myTimezone}, // Create an object /* A serialized repesentation of an object, suitable for an Ajax Request */ myData = jQuery.param(params); /* The Ajax Request */ $.ajax({ type: "post", url: "myDateScript.php", data: myData, // The data that is being sent to myDateScript.php success: function(info) { $('#result').html(info); // Display the result back when saved: } }); // End of Ajax Request: }); // End of Doc Ready: </script> </body> </html> The PHP <?php // Date & time format. $date_format = "m/d/Y"; $time_format = "h:i A"; // Set timezone. $timezone = $_POST['timezone']; // The Ajax Request $_POST['timezone']: //$timezone = America/New_York'; // Get current datetime. $date = new DateTime(); // Set timezone. $date->setTimezone(new DateTimeZone($timezone)); // Echo current date and time... plus send the response back: echo $date->format($date_format . " " . $time_format); Maybe the scripts can modify it to fit what he wants?
  23. Well, in that case : http://pellepim.bitbucket.org/jstz/
  24. You might want to Google jsTimezoneDetect that is a jQuery plugin that determines Timezone based on the user's location. I would give the link here, but for some reason one of the moderator's said I was going against PHP Freaks policies when I gave a link the last time..
  25. I was bored, it isn't perfect but it works and needs to be stylized: <?php /* I would pull the form and table apart, by that I mean */ /* don't try to do two things at once. I haven't done */ /* any number validation and I'm sure this needs to be */ /* modified for I don't know what exactly the initial */ /* HTML looked liked. */ if ( isset($_POST['submit']) && $_POST['submit'] == "Generate" ) { $numRows = $_POST['rows']; $numCols = $_POST['columns']; $operand = $_POST['operation']; } ?> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Learning How to Multiply & Add</title> <style> table { border-collapse: collapse; } table, th, td { border: 1px solid #2e2e2e; } </style> </head> <body> <form action="" method="post"> <label for="numRows" class="labelStyle">Number of Rows:</label> <input id="numRows" name="rows" value=""> <label for="numCols" class="labelStyle">Number of Cols:</label> <select id="numCols" name="columns"> <option value="1">1</option> <option value="2">2</option> <option value="4">4</option> <option value="8">8</option> <option value="16">16</option> </select> <br> <h3>Operation</h3> <input id="multiplication" type="radio" name="operation" value="multiplication" checked> <label for="multiplication">Multiplication</label> <input id="addition" type="radio" name="operation" value="addition"> <label for="addition">Addition</label> <br> <input class="submitBtn" type="submit" name="submit" value="Generate"> </form> <table class="tableStyle"> <thead> <tr> <!-- Number of Colums and Rows are initial set to 10 --> <?php $columns = isset($numCols) ? $numCols : 10; ?> <th colspan="<?php echo $columns ?>"><?php echo $columns . " Columns"; ?></th> </tr> <tr> <?php for($x = 0; $x < $columns; $x++) { echo '<th>' . $x . '</th>' . "\n"; } ?> </tr> </thead> <tbody> <?php $rows = isset($numRows) ? $numRows : 10; for($x = 0; $x < $rows; $x++) { echo "<tr>\n"; for($y = 0; $y < $columns; $y++) { /* If operand isn't set yet, I set it to an addition default */ if ( isset($operand) && ($operand == "multiplication")) { echo "<td>" . $x . " X " . $y . " = " . ($x * $y) . "</td>\n"; } elseif ( isset($operand) && ($operand == "addition") ) { echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n"; } else { echo "<td>" . $x . " + " . $y . " = " . ($x + $y) . "</td>\n"; } } echo "</tr>\n"; } ?> </tbody> </table> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.