Jump to content
Stef's Coding Community


  • Content Count

  • Joined

  • Last visited

  • Days Won


Everything posted by DavidCampbell

  1. I had some time to return to this today - fixed it. The bug arrises because of the subtle difference between get_data in the Template class. in the oopLogin the method does not have the option to echo out the data, whereas the one in phpCart, it does. so much confusion over something so simple.
  2. Hi Stefan, I haven't solved it yet, I'm a little stumped. My local version of OOP Login works as expected. Obviously there are differences in the file structure and the use of DPO instead of MYSQLI. My next move was to just refactor the code and use MYSQLI. But It would be nice to get DPO working though.
  3. the admin index.php, is very simple: <?php // ==================================================== // INDEX // // ==================================================== /* * Are we loged in? * YES - redirect to admin home * NO - redirect to login */ include("app/init.php"); // check authorization if ($Auth->checkLoginStatus() == FALSE) { $Template->redirect('login.php'); } else { echo "redirect to home.php"; // $Template->redirect('home.php'); }
  4. v_login.php (view) <!DOCTYPE html> <html> <head> <title>Login In</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <link href="resources/css/style.css" media="screen" rel="stylesheet" type="text/css"> </head> <body> <h1>Log In</h1> <div id="content"> <form action="" method="post"> <div> <? $alerts = $this->get_alerts(); if ($alerts != '') { echo "<ul class=\"alerts\">\n"; echo $alerts; echo "</ul>\n"; } ?> </div> <div class="row"> <label for="username">Username: *</label> <input type="text" name="username" value="<? echo $this->get_data('input_user'); ?>"> <div class="error"><? echo $this->get_data('error_user'); ?></div> </div> <div class="row"> <label for="password">Password: *</label> <input type="password" name="password" value="<? echo $this->get_data('input_pass'); ?>"> <div class="error"><? echo $this->get_data('error_pass'); ?></div> </div> <div class="row"> <p class="required">* required</p> <input type="submit" name="submit" class="submit" value="submit"> </div> </form> </div> </body> </html>
  5. login.php <?php // ==================================================== // LOGIN // // ==================================================== include("app/init.php"); $Template->set_data('page_class', 'login'); if (isset($_POST['submit'])) { // get data $Template->set_data('input_user', $_POST['username']); $Template->set_data('input_pass', $_POST['password']); // validate data if ($_POST['username'] == '' || $_POST['password'] == '') { // show error if ($_POST['username'] == '') { $Template->set_data('error_user', 'required');} if ($_POST['password'] == '') { $Template->set_data('error_pass', 'required');} $Template->set_alert('Please fill in all required fields', 'error'); $Template->load("app/views/v_login.php", "Login"); } else if ($Auth->validateLogin($Template->get_data('input_user'), $Template->get_data('input_pass')) == FALSE ) { // invalid login $Template->set_alert('Invalid username or password', 'error'); $Template->load("app/views/v_login.php", "Login"); } else { // successful log in $_SESSION['username'] = $Template->get_data('input_user'); $_SESSION['loggedin'] = TRUE; $Template->set_alert('Welcome <i>' . $Template->get_data('input_user') . '</i>'); $Template->redirect('home.php'); // send to admin home } } else { // echo "template load v_login.php"; $Template->load("app/views/v_login.php", "Login"); }
  6. auth.php <?php // ==================================================== // AUTHORIZATION CLASS // Deals with auth tasks // ==================================================== class Auth { // ========================== // Class scope vars // ========================== private $salt = 'j4H9?s0d'; // ========================== // Constructor // ========================== function __construct() { // do nothing } // ========================== // Methods // ========================== // ============= // validate login // ============= function validateLogin($user, $pass) { /* // access db global $Database; // create query if ($stmt = $Database->prepare("SELECT * FROM users WHERE username = ? AND password = ?")) { $passPlusSalt = $pass . $this->salt; $passPlusSalt = md5($passPlusSalt); $stmt->bind_param("ss", $user, $passPlusSalt); $stmt->execute(); $stmt->store_result(); // check for num rows if ($stmt->num_rows > 0) { // success $stmt->close(); return TRUE; } else { // failure $stmt->close(); return FALSE; } } else { die("ERROR: Could not prepare MySQLi statement."); } */ $sql = "SELECT * FROM users WHERE userName = :user AND password = :passPlusSalt AND adminUser = :isAdmin"; $user = $user; $passPlusSalt = $pass . $this->salt; $passPlusSalt = md5($passPlusSalt); $isAdmin = "true"; // access database global $connection; try { $statement = $connection->prepare($sql); $statement->bindParam(':user', $user, PDO::PARAM_STR); $statement->bindParam(':passPlusSalt', $passPlusSalt, PDO::PARAM_STR); $statement->bindParam(':isAdmin', $isAdmin, PDO::PARAM_STR); $statement->execute(); $result = $statement->fetchAll(); } catch(PDOException $error) { echo $sql . "<br>" . $error->getMessage(); } if ($result && $statement->rowCount() > 0) { // success return TRUE; } else { // failure return FALSE; } } // ============= // check login status // ============= function checkLoginStatus() { if (isset($_SESSION['loggedin'])) { return TRUE; } else { return FALSE; } } // ============= // logout // ============= function logout() { session_destroy(); session_start(); } // END Auth Class }
  7. template.php <?php // ==================================================== // TEMPLATE CLASS // Handling all templating tasks - displaying views, alerts, erros and view data // ==================================================== class Template { // ========================== // Class scope vars // ========================== private $data; private $alert_types = array('success', 'error'); // ========================== // Constructor // ========================== function __construct() { // do nothing } // ========================== // Methods // ========================== /** * Loads specified url * * @access public * @param string, string * @return null **/ public function load($url, $title) { if($title != ''){ $this->set_data('page_title', $title); } include($url); } /** * Redirects to specified url * * @access public * @param string * @return null **/ public function redirect($url) { header("Location: $url"); exit; } /* Get / Set Data */ /** * Saves provided data for use by the view later * * @access public * @param string, string, bool * @return null **/ public function set_data($name, $value, $clean = FALSE) { if ($clean == TRUE) { $this->data[$name] = htmlentities($value, ENT_QUOTES); } else { $this->data[$name] = $value; } } /** * Retrieves data based on provided name for access by view * * @access public * @param string, bool * @return string **/ public function get_data($name, $echo = TRUE) { if(isset($this->data[$name])) { if($echo) { echo $this->data[$name]; } else { return $this->data[$name]; } } return ''; } /* Get / Set Alerts */ /** * Sets an alert message stored in the session * * @access public * @param string, string (optional) * @return null **/ public function set_alert($value, $type = 'success') { $_SESSION[$type][] = $value; } /** * Returns string, containing multiple list items of alerts * * @access public * @param * @return string **/ public function get_alerts() { $data = ''; foreach ($this->alert_types as $alert) { if(isset($_SESSION[$alert])) { foreach ($_SESSION[$alert] as $value) { $data .= '<li class="' . $alert . '">' . $value . '</li>'; } unset($_SESSION[$alert]); } } // echo $data; return $data; } // END Template Class }
  8. <?php // =========================================== // INIT // Basic configuration settings // =========================================== // connect to database (PDO) $host = "localhost"; $username = "root"; $password = "yes"; $dbname = "recordLabel"; $dsn = "mysql:host=$host;dbname=$dbname"; $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); $connection = new PDO($dsn, $username, $password, $options); // set up constants define('SITE_NAME', 'My Record Label'); define('SITE_PATH', 'http://recordLabel.local/admin/'); // local/admin define('IMAGE_PATH', 'http://recordLabel.local/admin/resources/images/'); // local/admin // include objects include('app/models/m_template.php'); include('app/models/m_auth.php'); // create objects $Template = new Template(); $Auth = new Auth(); session_start(); my init.php
  9. Here is a screenshot of the bug: This login is based on the KillerSites OOP Login tutorial. But I've made some differences. Firstly is in a sub directory 'admin' as i'm building that first. Secondly the most important change is I've changed it to PDO instead of MySQLi. I've gone all through my code and cant see where this would decide to print out like this. Feel a bit dumb not being able to find out why this is performing like this. Hoping someone has a quick answer. I'm just at the beginning of this build and I wanted to adapt the code to use DPO, although I have had no experience with DPO. So I was thinking that is prob the route of the problem. Otherwise I'll switch back to MYSQLI, which I want to avoid.
  10. You tell them that you are a web developer, not an SEO consultant. Go hire an SEO consultant. If they want you to do it, there's an extra charge - get them to pay you a monthly retainer to manage their SEO needs. If they don't like the results they can cancel anytime. This falls into the bracket of managing customer expectations. Also putting people off getting you do silly work. If you don't want to do it, put your price up so they don't ask you to do it... or you're happy to do it because you're getting paid well.
  11. https://www.youtube.com/results?search_query=photoshop
  12. your style sheet should not have this syntax: <style> </style> just do it like this: body { background: url("images/background-forum.jpg"); } and include it in your html head: <link href="pathTo/style.css" media="all" rel="stylesheet" type="text/css">
  13. actually I think I've solved it: BigDecimal a00 = new BigDecimal(a[0][0]); BigDecimal a01 = new BigDecimal(a[0][1]); BigDecimal a02 = new BigDecimal(a[0][2]); BigDecimal a10 = new BigDecimal(a[1][0]); BigDecimal a11 = new BigDecimal(a[1][1]); BigDecimal a12 = new BigDecimal(a[1][2]); BigDecimal a20 = new BigDecimal(a[2][0]); BigDecimal a21 = new BigDecimal(a[2][1]); BigDecimal a22 = new BigDecimal(a[2][2]); BigDecimal a00a11a22 = new BigDecimal("0.0"); BigDecimal a01a12a20 = new BigDecimal("0.0"); BigDecimal a02a10a21 = new BigDecimal("0.0"); BigDecimal a20a11a02 = new BigDecimal("0.0"); BigDecimal a21a12a00 = new BigDecimal("0.0"); BigDecimal a22a10a01 = new BigDecimal("0.0"); BigDecimal bigDecimalDeterminantMinuend = new BigDecimal("0.0"); BigDecimal bigDecimalDeterminantSubtrahend = new BigDecimal("0.0"); a00a11a22 = a00.multiply(a11).multiply(a22); a01a12a20 = a01.multiply(a12).multiply(a20); a02a10a21 = a02.multiply(a10).multiply(a21); bigDecimalDeterminantMinuend = a00a11a22.add(a01a12a20).add(a02a10a21); a20a11a02 = a20.multiply(a11).multiply(a02); a21a12a00 = a21.multiply(a12).multiply(a00); a22a10a01 = a22.multiply(a10).multiply(a01); bigDecimalDeterminantSubtrahend = a20a11a02.add(a21a12a00).add(a22a10a01);
  14. anyone know how to multiply multiple BigDecimals? not like this example: BigDecimal subTotal, taxRate, tax, total; subTotal = new BigDecimal("32.50"); taxRate = new BigDecimal("0.05"); tax = subTotal.multiply(taxRate); total = subTotal.add(tax); I need to multiple more than two numbers and also do addition. This is the line i'm trying to convert to BigDecimals: double aDeterminantMinuend = (a[0][0] * a[1][1] * a[2][2]) + (a[0][1] * a[1][2] * a[2][0]) + (a[0][2] * a[1][0] * a[2][1]); as you can see those are doubles in an array that i'm doing my arithmetic to. so I've prepped them like: BigDecimal a00 = new BigDecimal(a[0][0]); BigDecimal a01 = new BigDecimal(a[0][1]); BigDecimal a02 = new BigDecimal(a[0][2]); BigDecimal a10 = new BigDecimal(a[1][0]); BigDecimal a11 = new BigDecimal(a[1][1]); BigDecimal a12 = new BigDecimal(a[1][2]); BigDecimal a20 = new BigDecimal(a[2][0]); BigDecimal a21 = new BigDecimal(a[2][1]); BigDecimal a22 = new BigDecimal(a[2][2]); so now I need to do the BigDecimal arithmetic to them, but the only examples i can find are like the one posted above. Just two BigDecimals multiplied together. i've tried embedding multiple calls the multiple() method, but that doesnt work. I'm thinking the only way to do it is to make new BigDecimals for each step of the arithmetic, but surely theres a better, neater way?
  15. when you open a <div> always close the div </div> I like to mark my closing divs, so I don't lose track. In PHP I would have comment like: // END div.classOfDiv print "</div>"; or in html: <!-- END div.classOfDiv --> </div> yes you can embed divs within other divs.
  16. I disagree with the don't use classes comment. No! Use classes all the time, not id's. It's rare that you are styling one specific element. Maybe something like a logo. But most things are going to be groups of elements, so use a class for that group, for that "class" of element. Thats what its for. If you want to minimise the classes you're adding to elements, just add a class to the one element that is the parent to a group (in the DOM), usually a div. Then you can use that to target all elements within. div.myListItem p Styles all paragraphs within any div with a class of 'myListItem'.
  17. What do we think of Brian Will's assertion that Object-Oriented Programming is Bad?
  18. With photoshop don't get too attached to the details, as once you start coding, things tend to look different. Especially once you start accounting for the way different browsers interpret your beautifully crafted design. Being pedantic about pixel perfect design will just lead you to despair. You need to think of design as fluid, it's not like graphic design for print. Not at all. Don't be scared of using CSS, the work flow can be very fast once you know what you're doing. Just as fast as fumbling around in photoshop or illustrator. I'm in the camp of preferring to dive straight in with the code. UX problems i'll just sketch out with pencil and paper roughly, then go code it. I use photoshop to appease people who need to see some eye candy. But I manage expectations by stating that what you see in photoshop is not necessarily how the final implementation will look.
  19. Videos: Composer Introduction & SDK Setup PayPal Settings & Minor Changes Steps to Submitting a Payment Integrating PayPal Part 1 Integrating PayPal Part 2 Integrating PayPal Part 3 Integrating PayPal Part 4 On the whole the videos can be followed, but there are few red herrings on the way. And unnecessary distractions. Composer Introduction & SDK Setup Forget Composer, this is an unnecessary distraction and just confuses things, maybe mention Composer at the end of the tutorial as an alternative method for installing the SDK. Get the SDK hosted on Github from here: https://github.com/paypal/PayPal-PHP-SDK/releases I used the 1.13.0 zip (latest version) from this page and it works fine. To follow the videos you would rename the uncompressed SDK folder to 'vendor' and place it in the /app directory. The 'samples' are slightly different. They are now hosted online here" https://paypal.github.io/PayPal-PHP-SDK/sample/ Only one sample is actually used in the tutorial, this one: https://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html as you can see it looks virtually the same. If you want to host the samples locally, you can still do that. Go to the main SDK repository on Github https://github.com/paypal/PayPal-PHP-SDK you can see the samples directory is there. I did however have trouble downloading directly from here. For some reason when you hit the clone/download button and download from here the zip is missing files and directories you'd expect to be there, including the samples directory. I have no clue why this is. I managed to work around this problem by forking the repository and using the GitHub desktop app to clone my fork to my local drive. It was then fairly simple to drag the 'samples' directory out and host that with my MAMP/LAMP. PayPal Settings & Minor Changes Do the changes to init.php etc Then the first step really is to goto https://developer.paypal.com/ log in and go to the dashboard (it all looks very different, but it's essentially doing the same job as before). Scroll down to REST API apps and create a new App. A client ID, secret and two test accounts (buyer and facilitator) are created automatically. Continue and make the changes to init.php, v_public_cart.php, success.php, v_public_success.php and m_payments.php etc. Steps to Submitting a Payment This video is a bit confusing now, as the interactive guide is not the same. The vid really needs to be just a simple explanation of the steps used with paypal. Integrating PayPal Part 1 Integrating PayPal Part 2 Integrating PayPal Part 3 Integrating PayPal Part 4 Once all the above is sorted you can follow these video's pretty much verbatim, only there are some subtle differences with the bootstrap.php file that you'll be copying from. It's easier to just look at my m_payments.php file: <?php /* Payments Class Handle all tasks related to payments */ require ('app/vendor/autoload.php'); use PayPal\Rest\ApiContext; use PayPal\Auth\OAuthTokenCredential; use PayPal\Api\Amount; use PayPal\Api\Details; use PayPal\Api\Item; use PayPal\Api\ItemList; use PayPal\Api\Payer; use PayPal\Api\Payment; use PayPal\Api\RedirectUrls; use PayPal\Api\Transaction; use PayPal\Api\PaymentExecution; class Payments { private $api_context; function __construct() { $this->api_context = $this->get_api_context(); // echo '<pre>'; // print_r($this->api_context); // echo '</pre>'; // exit; } /* Getters and Setters */ public function get_api_context() { if (PAYPAL_MODE == "sandbox") { $apiContext = new ApiContext( new OAuthTokenCredential ( PAYPAL_DEVID, PAYPAL_DEVSECRET ) ); } else { $apiContext = new ApiContext( new OAuthTokenCredential ( PAYPAL_LIVEID, PAYPAL_LIVESECRET ) ); } $apiContext->setConfig(array ( 'mode' => PAYPAL_MODE, 'http.ConnectionTimeOut' => 30, 'log.LogEnabled' => true, 'log.FileName' => 'app/PayPal.log', 'log.LogLevel' => 'FINE' )); return $apiContext; } /** * Creates PayPal payment: * * @access public * @param * @return error string **/ public function create_payment($items_array, $details_array) { $payer = new Payer(); $payer->setPaymentMethod("paypal"); // set items $i = 0; foreach ($items_array as $item) { $items[$i] = new Item(); $items[$i] ->setName($item['name']) ->setCurrency(PAYPAL_CURRENCY) ->setQuantity($item['quantity']) ->setSku("123123" . $i) ->setPrice($item['price']); $i++; } $itemList = new ItemList(); $itemList->setItems($items); // set details $details = new Details(); $details ->setShipping($details_array['shipping']) ->setTax($details_array['tax']) ->setSubtotal($details_array['subtotal']); // set amount $amount = new Amount(); $amount ->setCurrency(PAYPAL_CURRENCY) ->setTotal($details_array['total']) ->setDetails($details); // set transaction $transaction = new Transaction(); $transaction ->setAmount($amount) ->setItemList($itemList) ->setDescription("") ->setInvoiceNumber(uniqid()); // create urls $redirectUrls = new RedirectUrls(); $redirectUrls ->setReturnUrl(SITE_PATH . "success.php") ->setCancelUrl(SITE_PATH . "cart.php"); // create payment $payment = new Payment(); $payment ->setIntent("sale") ->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions(array($transaction)); try { $payment->create($this->api_context); } catch (Exception $ex) { // echo '<pre>'; // print_r($ex->getData()); // echo '</pre>'; // exit; return $ex->getMessage(); } // get redirect url $approvalUrl = $payment->getApprovalLink(); $_SESSION['payment_id'] = $payment->getId(); if (isset($approvalUrl)) { header("Location: $approvalUrl"); exit; } } /** * Executes PayPal payment: * * @access public * @param string, string * @return result object **/ public function execute_payment($payer_id, $payment_id) { $payment = Payment::get($payment_id, $this->api_context); $execution = new PaymentExecution(); $execution->setPayerId($payer_id); $result = $payment->execute($execution, $this->api_context); return $result; } } As you can see it's almost the same, just some subtle difference at the bottom with the re-directs and the try/catch. I did have to fix that bug that was a result of a comma being in the amount (see earlier post for the fix). Anyway I think that was everything. Dave
  20. No worries Stef, I'll write up some instructions. I have it all working as expected.
  21. bug fixed: here is my new checkout.php <?php include('app/init.php'); if(isset($_POST)) { // create Payment Object include('app/models/m_payments.php'); $Payments = new Payments(); // get item data $items = $Cart->get(); // get details $details['subtotal'] = $Cart->get_total_cost(); $details['shipping'] = 0; foreach ($items as $item) { $details['shipping'] += $Cart->get_shipping_cost($item['price']); } $details['shipping'] = number_format($details['shipping'], 2, '.', ''); $details['tax'] = number_format($details['subtotal'] * SHOP_TAX, 2, '.', ''); $details['total'] = number_format( $details['subtotal'] + $details['shipping'] + $details['tax'], 2, '.', ''); // send to PayPal $error = $Payments->create_payment($items, $details); if ($error != NULL) { $Template->set_alert($error, 'error'); $Template->redirect('cart.php'); } } else { $Template->redirect('cart.php'); } note the changes to number_format() where appropriate.
  22. Any progress on this course idea? I've been doing JAVA, and it wasnt until I started learning Java that I really "got" programming. I had played with PHP before that. Anyway I fully intend to become a good Java programmer, because it's the highest paying here in the UK. If freelancing/being an entrepreneur doesn't work out. Although a computer science degree seems to be required for a lot of these Java jobs.
  23. Well I've kinda got it working, only it doesnt like me buying 10 TV's LOL. I can buy 2 headphones though, so not sure whats going with the quantity bug. This is the error I get: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Total is not a valid numeric value' in /Volumes/5TB_Seagate/htdocs/phpCart/app/vendor/paypal/rest-api-sdk-php/lib/PayPal/Validation/NumericValidator.php:23 Stack trace: #0 /Volumes/5TB_Seagate/htdocs/phpCart/app/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/Amount.php(54): PayPal\Validation\NumericValidator::validate('3,302.39', 'Total') #1 /Volumes/5TB_Seagate/htdocs/phpCart/app/models/m_payments.php(132): PayPal\Api\Amount->setTotal('3,302.39') #2 /Volumes/5TB_Seagate/htdocs/phpCart/checkout.php(31): Payments->create_payment(Array, Array) #3 {main} thrown in /Volumes/5TB_Seagate/htdocs/phpCart/app/vendor/paypal/rest-api-sdk-php/lib/PayPal/Validation/NumericValidator.php on line 23
  • Create New...