Jump to content
Larry Ullman's Book Forums

kravmaguy

Members
  • Posts

    10
  • Joined

  • Last visited

Posts posted by kravmaguy

  1. Your explanation clarified this for me thank you. And It makes alot of sense now. 

    However can you please give your opinion on this thread : 

    https://stackoverflow.com/questions/3230133/accessing-cookie-immediately-after-setcookie

    The first explanation makes sense he says that if you want immediate access, then you would do the following:

    setcookie('uname', $uname, time()+60*30);
    $_COOKIE['uname'] = $uname;
    

    This would be manually setting it, and I was curious why the method you outlined is better because if we used this, then we can modify that extra piece of validation code in the footer that checks if the page we are on is not login or logout. So setting it manually would be less secure? 

  2. there is a function isadministrator() in an includes file which checks for the existence of a certain cookie, yet in the footer there are   exceptions in his logic for the login and logout page.

    can someone please go over with me why its not working on these two pages:
    on login.php : basically when the pass and user is correct the server sends a cookie to the client but its not available to be read right away unless you refresh the page?

    on logout,php:
    destroys the cookie by setting it to false and its time in the past…
    so then why wouldnt isadministrator work? why does the browser still think the cookie exists? is this the same reason as on login.php page?

     

  3. On 3/20/2018 at 10:48 PM, einthetheory said:

    I'm working with the last chapter in book trying to put it all together but, the edit quotes.php and delete quotes.php, gives me this error below.

    "This page has been accessed in error."  

     

    I made sure that I'm logged in using the me@example.com and testpass password and even checked my scripts with yours I think everything lined up. Can you please help?

     

    Edit_quotes.php Script 

     

     

    
    <?php // Script 13.9 - edit_quote.php
    /* This script edits a quote. */
    // Define a page title and include the header:
    define('TITLE', 'Edit a Quote');
    include('templates/header.html');
    print '<h2>Edit a Quotation</h2>';
    // Restrict access to administators only:
    if (!is_administrator()) {
     print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>';
     include('templates/footer.html');
     exit();
    }
    // Need the database connection:
    include('../mysqli_connect.php');
    if (isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'] > 0) ) { // Display the entry in a form:
     // Define the query.
     $query = "SELECT quote, source, favorite FROM quotes WHERE id={$_GET['id']}";
     if ($result = mysqli_query($dbc, $query)) { // Run the query.
     $row = mysqli_fetch_array($result); // Retrieve the information.
    
      // Make the form:
      print '<form action="edit_quote.php" method="post">
       <p><label>Quote <textarea name="quote" rows="5" cols="30">' . htmlentities($row['quote']) . '</textarea></label></p>
       <p><label>Source <input type="text" name="source"value="' . htmlentities($row['source']) . '"></label></p>
       <p><label>Is this a favorite? <input type="checkbox" name="favorite" value="yes"';
      // Check the box if it is a favorite:
      if ($row['favorite'] == 1) {
       print ' checked="checked"';
      }
      // Complete the form:
      print '></label></p>
       <input type="hidden" name="id" value="' . $_GET['id'] . '">
       <p><input type="submit" name="submit" value="Update This Quote!"></p>
     </form>';
     } else { // Couldn't get the information.
      print '<p class="error">Could not retrieve the quotation because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
     }
    } elseif (isset($_POST['id']) && is_numeric($_POST['id']) && ($_POST['id'] > 0)) { // Handle the form.
     // Validate and secure the form data:
     $problem = FALSE;
     if ( !empty($_POST['quote']) && !empty($_POST['source']) ) {
      // Prepare the values for storing:
      $quote = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['quote'])));
      $source = mysqli_real_escape_string($dbc, trim(strip_tags($_POST['source'])));
      // Create the "favorite" value:
      if (isset($_POST['favorite'])) {
       $favorite = 1;
      } else {
       $favorite = 0;
      }
     } else {
      print '<p class="error">Please submit both a quotation and a source.</p>';
      $problem = TRUE;
     }
     if (!$problem) {
      // Define the query.
      $query = "UPDATE quotes SET quote='$quote', source='$source', favorite=$favorite WHERE id={$_POST['id']}";
      if ($result = mysqli_query($dbc, $query)) {
       print '<p>The quotation has been updated.</p>';
      } else {
       print '<p class="error">Could not update the quotation because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
      }
     } // No problem!
    } else { // No ID set.
     print '<p class="error">This page has been accessed in error.</p>';
    } // End of main IF.
    mysqli_close($dbc); // Close the connection.
    include('templates/footer.html'); // Include the footer.
    ?>

     

     

    Delete_quotes.php Script 


     

    
    
    <?php // Script 13.10 - delete_quote.php
    /* This script deletes a quote. */
    // Define a page title and include the header:
    define('TITLE', 'Delete a Quote');
    include('templates/header.html');
    print '<h2>Delete a Quotation</h2>';
    // Restrict access to administrators only:
    if (!is_administrator()) {
     print '<h2>Access Denied!</h2><p class="error">You do not have permission to access this page.</p>';
     include('templates/footer.html');
     exit();
    }
    // Need the database connection:
    include('../mysqli_connect.php');
    if (isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'] > 0) ) { // Display the quote in a form:
     // Define the query:
     $query = "SELECT quote, source, favorite FROM quotes WHERE id={$_GET['id']}";
     if ($result = mysqli_query($dbc, $query)) { // Run the query.
      $row = mysqli_fetch_array($result); // Retrieve the information.
      // Make the form:
      print '<form action="delete_quote.php" method="post">
      <p>Are you sure you want to delete this quote?</p>
      <div><blockquote>' . $row['quote'] . '</blockquote>- ' . $row['source'];
      // Is this a favorite?
      if ($row['favorite'] == 1) {
       print ' <strong>Favorite!</strong>';
      }
      print '</div><br><input type="hidden" name="id" value="' . $_GET['id'] . '">
      <p><input type="submit" name="submit" value="Delete this Quote!"></p>
      </form>';
     } else { // Couldn't get the information.
      print '<p class="error">Could not retrieve the quote because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
     }
    } elseif (isset($_POST['id']) && is_numeric($_POST['id']) && ($_POST['id'] > 0) ) { // Handle the form.
     // Define the query:
     $query = "DELETE FROM quotes WHERE id={$_POST['id']} LIMIT 1";
     $result = mysqli_query($dbc, $query); // Execute the query.
     // Report on the result:
     if (mysqli_affected_rows($dbc) == 1) {
      print '<p>The quote entry has been deleted.</p>';
     } else {
      print '<p class="error">Could not delete the blog entry because:<br>' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
     }
    } else { // No ID received.
     print '<p class="error">This page has been accessed in error.</p>';
    } // End of main IF.
    mysqli_close($dbc); // Close the connection.
    include('templates/footer.html');
    ?>

    I reformatted OP's code so its easier for everyone else to read

    On 3/20/2018 at 10:48 PM, einthetheory said:

     

     

  4.  It actually looks excellent on edge.. On firefox and chrome it looks horrible;

     I think i added some margins on the version in chapter 8 it made it look better, but its only on one of the pages, i didnt do it on every page, how can we fix this? 

    edit: for some reason now its looking much better on chrome and firefox, cant remember what changes i made, but the problem of the no padding is now gone and its looking better across all three browsers. I will update this thread if any changes occur. 

     

  5. I copied and pasted the concise.min code from github but i noticed a few odd things:

    There is no side margin when viewed on 100% only when its viewed on 80% see attached screenshot

    im using brackets and normally brackets will color code the text that it recognizes this is fine until halfway through the file, however the last half of the file is still in black text, why is that? see attached screenshot

    another student on the forums suggested exchanging the link to the cdn instead of using a local file, which i did, but now the site looks even wierder. See screenshot. 

    So im not sure if the site is supposed to be looking like this or if ive made a mistake somewhere? 

    I see im past the filesize allotment for this forum so here is a link to a public folder where i put the three screenshots:

    https://drive.google.com/drive/folders/1hPrkxu_EhFwnON24RJxWgy6hgDBgtDLd?usp=sharing

  6. I put in this code at the end of the exercise to try to help me understand it better, 

    1. when I print $books it gives me the entire array this i expected

    2. print_r($book); only gives me the Last book? 

    3. print_r($chapter); prints all the books?

    4. print_r($key); prints only the last key in the $chapters array which is really a book? 

    5. print_r($value); prints only the last value in the $chapters array which is value of the last $key

    I am hoping someone can explain this to me and why it does this. 

    
    foreach ($books as $book => $chapter) {
    	print "<p>$book";
    		foreach ($chapter as $key => 
    		$value){
    		print "</br>chapter $key is $value";
    		}
    	print "</p>";
    
    }
    
    echo '<pre>';
    print_r($books);
    echo  '</pre>';
    
    echo '<pre>';
    print_r($book);
    echo  '</pre>';
    
    echo '<pre>';
    print_r($chapter);
    echo  '</pre>';
    
    echo '<pre>';
    print_r($key);
    echo  '</pre>';
    
    
    echo '<pre>';
    print_r($value);
    echo  '</pre>';
    ?>

     

×
×
  • Create New...