Jump to content
Larry Ullman's Book Forums

grahamgr3

Members
  • Posts

    99
  • Joined

  • Last visited

Posts posted by grahamgr3

  1. My show_image.php script is not working. When I click on the links the popup window comes up but the images don't display, only the unavailable.jpg image displays. I checked the uploads folder and it seems ok. I am using the downloaded script from this site to make sure the code is ok. For images.php, and upload_image.php, and functions.js,  I am also using this site's scripts. I tried to see the source code from the popup window and there is nothing it is blank. 

     

    Here is the show_images.php code.

    <?php # Script 11.5 - show_image.php
    // This page displays an image.
    
    $name = FALSE; // Flag variable:
    
    // Check for an image name in the URL:
    if (isset($_GET['image'])) {
    
    	// Make sure it has an image's extension:
    	$ext = strtolower ( substr ($_GET['image'], -4));
    	
    	if (($ext == '.jpg') OR ($ext == 'jpeg') OR ($ext == '.png')) {
    
    		// Full image path:
    		$image = "../../../../uploads/{$_GET['image']}";
    
    		// Check that the image exists and is a file:
    		if (file_exists ($image) && (is_file($image))) {
    			
    			// Set the name as this image:
    			$name = $_GET['image'];	
    
    		} // End of file_exists() IF.
    
    	} // End of $ext IF.
    	
    } // End of isset($_GET['image']) IF.
    
    // If there was a problem, use the default image:
    if (!$name) {
    	$image = 'images/unavailable.png';	
    	$name = 'unavailable.png';
    }
    
    // Get the image information:
    $info = getimagesize($image);
    $fs = filesize($image);
    
    // Send the content information:
    header ("Content-Type: {$info['mime']}\n");
    header ("Content-Disposition: inline; filename=\"$name\"\n");
    header ("Content-Length: $fs\n");
    
    // Send the file:
    readfile ($image);
    
  2. In Chapter 11 in the sending email script called email.php, the $_POST['name'] and $_POST['comments'] in the $body variable are wrapped in curly braces. Why are there curly braces around them. 

    See the code below.

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    	if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments'])){
    		$body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";
    		$body = wordwrap($body, 70);
    	}
    }
    
  3. I am trying to do the pursue question that says modify edit_user.php so that the form elements' values come from $_POST, if set and the database if not.

    Here is what I came up with but I get an error message saying

    Parse error: syntax error, unexpected 'if' (T_IF), expecting ',' or ';' in D:\xampp\htdocs\2book\ch9\edit_user.php on line 78

     

    Here is my code.

    $q = "SELECT first_name, last_name, email, pass FROM users WHERE user_id=$id";
    $r = @mysqli_query ($dbc, $q);
    if (mysqli_num_rows($r) == 1){
    	$row = mysqli_fetch_array($r, MYSQLI_NUM);
    echo '<form action="edit_user.php" method="post">
    <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="'
    if (isset($_POST['first_name'])){
     echo $_POST['first_name']; 
    } else { 
    echo  $row[0];
    } '"/></p>
    <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p>
    <p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="' . $row[2] . '" /></p>
    <p>Password: <input type="password" name="pass1" size="10" maxlength="40" value="' . $row[3] . '" /></p>
    <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="40" value="' . $row[3] . '" /></p>
    <p><input type="submit" name="submit" value="Submit" /></p>
    <input type="hidden" name="id" value="' . $id . '" /></form>';
    } else {
    	echo '<p class="error">This page has been accessed in error.</p>';
    }
    

     

  4. I will better explain my question so that it will deserve a proper answer. 

     

    First at the top of my script I am validating that the field SiteTypeID is ok. 

    if (!empty($scrubbed['SiteTypeID'])){
    		$sitetypeid = $scrubbed['SiteTypeID'];
    	} else {
    		$sitetypeid ="";
    		echo '<p class="error">Please select a category</p>';
    	}
    

    Then at the bottom of the script I have a select menu that is populated with an associative array and a foreach loop. However the $key variable is not working. It does not have a value between 3 and 43 like it should. Instead it's value is always 0. I want to know why it doesn't have the number value like it the keys in my associative array. 

     

    Here is the code that shows the array and the select menu and the foreach loop.

    $optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
    18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
    26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
    echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
    foreach ($optionarray as $key => $value){
    	echo '<option value="$key"';
    	if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key));
    echo 'selected="selected">'; 
    	echo $value . '</option>';
    }
    echo '</select>';
    

  5. <form action="edit_your_sites.php" method="post">
    Please select a category:<select name="SiteTypeID">
    <option value="">Select a Category</option>
    <option value="5"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '5')) echo ' selected="selected"'; ?>>Advertising</option>
    <option value="6"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '6')) echo ' selected="selected"'; ?>>Affiliate Programs</option>
    <option value="7"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '7')) echo ' selected="selected"'; ?>>Art & Photography</option>
    <option value="9"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '9')) echo ' selected="selected"'; ?>>Beauty & Jewelry</option>
    <option value="10"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '10')) echo ' selected="selected"'; ?>>Blogging</option>
    <option value="11"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '11')) echo ' selected="selected"'; ?>>Books, Literature</option>
    <option value="12"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '12')) echo ' selected="selected"'; ?>>Business & Finance</option>
    <option value="3"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '3')) echo ' selected="selected"'; ?>>Children & Family</option>
    <option value="13"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '13')) echo ' selected="selected"'; ?>>Computer Games</option>
    <option value="14"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '14')) echo ' selected="selected"'; ?>>Computing</option>
    <option value="15"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '15')) echo ' selected="selected"'; ?>>Dating & Relationships</option>
    <option value="16"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '16')) echo ' selected="selected"'; ?>>Directories</option>
    <option value="17"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '17')) echo ' selected="selected"'; ?>>Education</option>
    <option value="18"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '18')) echo ' selected="selected"'; ?>>Electronics</option>
    <option value="19"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '19')) echo ' selected="selected"'; ?>>Entertainment</option>
    <option value="20"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '20')) echo ' selected="selected"'; ?>>Environment</option>
    <option value="21"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '21')) echo ' selected="selected"'; ?>>Flowers</option>
    <option value="22"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '22')) echo ' selected="selected"'; ?>>Food, Drink</option>
    <option value="23"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '23')) echo ' selected="selected"'; ?>>Forums, chat rooms</option>
    <option value="24"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '24')) echo ' selected="selected"'; ?>>Free Stuff</option>
    <option value="25"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '25')) echo ' selected="selected"'; ?>>Gifts & Shopping</option>
    <option value="26"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '26')) echo ' selected="selected"'; ?>>Health</option>
    <option value="4"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '4')) echo ' selected="selected"'; ?>>Home Business</option>
    <option value="27"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '27')) echo ' selected="selected"'; ?>>Humor</option>
    <option value="28"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '28')) echo ' selected="selected"'; ?>>Interior Design</option>
    <option value="29"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '29')) echo ' selected="selected"'; ?>>Internet Marketing</option>
    <option value="30"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '30')) echo ' selected="selected"'; ?>>Miscellaneous</option>
    <option value="31"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '31')) echo ' selected="selected"'; ?>>Music</option>
    <option value="32"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '32')) echo ' selected="selected"'; ?>>Pets</option>
    <option value="43"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '43')) echo ' selected="selected"'; ?>>Psychology</option>
    <option value="33"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '33')) echo ' selected="selected"'; ?>>Real Estate</option>
    <option value="34"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '34')) echo ' selected="selected"'; ?>>Religion & Spirituality</option>
    <option value="35"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '35')) echo ' selected="selected"'; ?>>Science</option>
    <option value="36"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '36')) echo ' selected="selected"'; ?>>Sports</option>
    <option value="37"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '37')) echo ' selected="selected"'; ?>>Stocks & Trading</option>
    <option value="38"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '38')) echo ' selected="selected"'; ?>>Travel</option>
    <option value="39"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '39')) echo ' selected="selected"'; ?>>Vehicles</option>
    <option value="40"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '40')) echo ' selected="selected"'; ?>>Web design</option>
    <option value="41"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '41')) echo ' selected="selected"'; ?>>Web hosting</option>
    <option value="42"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '42')) echo ' selected="selected"'; ?>>Work at Home</option>
    </select>

    Here is the code with the foreach loop:

    $optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education',
    18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping',
    26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
    echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
    foreach ($optionarray as $key => $value){
    echo '<option value="$key"';
    if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key))
    echo 'selected="selected">';
    echo $value . '</option>';
    }
    echo '</select>';

     

  6. I tried that and it shows that the array is empty. So post contains nothing. I am still unclear how to get my code to work though. What I am trying to do is replace a lot of hardcoded data with a foreach loop, however the key in my foreach loop isn't being selected and therefore my code isn't working. The problem might be with the selected="selected" part. Here is the hard-coded code, and below it is my new code with the foreach loop. The hardcoded code works perfectly. 

     

    <form action="edit_your_sites.php" method="post">
    Please select a category:<select name="SiteTypeID">
    <option value="">Select a Category</option>
    <option value="5"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '5')) echo ' selected="selected"'; ?>>Advertising</option>
    <option value="6"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '6')) echo ' selected="selected"'; ?>>Affiliate Programs</option>
    <option value="7"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '7')) echo ' selected="selected"'; ?>>Art & Photography</option>
    <option value="9"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '9')) echo ' selected="selected"'; ?>>Beauty & Jewelry</option>
    <option value="10"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '10')) echo ' selected="selected"'; ?>>Blogging</option>
    <option value="11"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '11')) echo ' selected="selected"'; ?>>Books, Literature</option>
    <option value="12"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '12')) echo ' selected="selected"'; ?>>Business & Finance</option>
    <option value="3"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '3')) echo ' selected="selected"'; ?>>Children & Family</option>
    <option value="13"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '13')) echo ' selected="selected"'; ?>>Computer Games</option>
    <option value="14"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '14')) echo ' selected="selected"'; ?>>Computing</option>
    <option value="15"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '15')) echo ' selected="selected"'; ?>>Dating & Relationships</option>
    <option value="16"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '16')) echo ' selected="selected"'; ?>>Directories</option>
    <option value="17"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '17')) echo ' selected="selected"'; ?>>Education</option>
    <option value="18"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '18')) echo ' selected="selected"'; ?>>Electronics</option>
    <option value="19"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '19')) echo ' selected="selected"'; ?>>Entertainment</option>
    <option value="20"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '20')) echo ' selected="selected"'; ?>>Environment</option>
    <option value="21"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '21')) echo ' selected="selected"'; ?>>Flowers</option>
    <option value="22"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '22')) echo ' selected="selected"'; ?>>Food, Drink</option>
    <option value="23"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '23')) echo ' selected="selected"'; ?>>Forums, chat rooms</option>
    <option value="24"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '24')) echo ' selected="selected"'; ?>>Free Stuff</option>
    <option value="25"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '25')) echo ' selected="selected"'; ?>>Gifts & Shopping</option>
    <option value="26"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '26')) echo ' selected="selected"'; ?>>Health</option>
    <option value="4"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '4')) echo ' selected="selected"'; ?>>Home Business</option>
    <option value="27"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '27')) echo ' selected="selected"'; ?>>Humor</option>
    <option value="28"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '28')) echo ' selected="selected"'; ?>>Interior Design</option>
    <option value="29"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '29')) echo ' selected="selected"'; ?>>Internet Marketing</option>
    <option value="30"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '30')) echo ' selected="selected"'; ?>>Miscellaneous</option>
    <option value="31"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '31')) echo ' selected="selected"'; ?>>Music</option>
    <option value="32"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '32')) echo ' selected="selected"'; ?>>Pets</option>
    <option value="43"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '43')) echo ' selected="selected"'; ?>>Psychology</option>
    <option value="33"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '33')) echo ' selected="selected"'; ?>>Real Estate</option>
    <option value="34"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '34')) echo ' selected="selected"'; ?>>Religion & Spirituality</option>
    <option value="35"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '35')) echo ' selected="selected"'; ?>>Science</option>
    <option value="36"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '36')) echo ' selected="selected"'; ?>>Sports</option>
    <option value="37"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '37')) echo ' selected="selected"'; ?>>Stocks & Trading</option>
    <option value="38"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '38')) echo ' selected="selected"'; ?>>Travel</option>
    <option value="39"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '39')) echo ' selected="selected"'; ?>>Vehicles</option>
    <option value="40"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '40')) echo ' selected="selected"'; ?>>Web design</option>
    <option value="41"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '41')) echo ' selected="selected"'; ?>>Web hosting</option>
    <option value="42"<?php if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '42')) echo ' selected="selected"'; ?>>Work at Home</option>
    </select>
     
    Here is the code with the foreach loop:
     
    $optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
    18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
    26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
    echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
    foreach ($optionarray as $key => $value){
    echo '<option value="$key"';
    if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == $key)) 
    echo 'selected="selected">'; 
    echo $value . '</option>';
    }
    echo '</select>';
  7. I am building a website after reading this book, and I am having trouble with the selected="selected" part of a select menu. Nothing is getting selected. When I click on submit form it goes through but the item I selected isn't being selected in my database. 

     

    In the following code, the problem lies in the following section: where the select menu is, but I can't get it to work. help would be appreciated. 

     

    <?php
    $optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
    18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
    26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
     
    echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
     
    foreach ($optionarray as $key => $value){
    echo '<option value="$key"';
    if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '. $key . ')) echo 'selected="selected">'; 
    echo $value . '</option>';
    }
    echo '</select>';
    ?>

     

     

     

     

     

     

     

    <?php
    $page_title = 'Edit Your Account';
    include ('includes/header.html');
    include ('includes/functions.php');
    include ('includes/config.inc.php');
    if (isset($_GET['id']) && is_numeric($_GET['id'])){
    $id = $_GET['id'];
    } elseif (isset($_POST['id']) && is_numeric($_POST['id'])) {
    $id = $_POST['id'];
    } else {
    echo '<p class="error">This page has been accessed in error.</p>';
    include ('includes/footer.html');
    exit();
    }
    if (isset($_SESSION['UserID'])){
    require (MYSQL);
    echo '<div class="text">';
    $scrubbed = array_map('spam_scrubber', $_POST);
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    if (filter_var($scrubbed['url'], FILTER_VALIDATE_URL)){
    $url = mysqli_real_escape_string($dbc, $scrubbed['url']);
    } else {
    $url = "";
    echo '<p class="error">Please enter a valid url</p>';
    }
    if (!empty($scrubbed['SiteTypeID'])){
    $sitetypeid = $scrubbed['SiteTypeID'];
    } else {
    $sitetypeid ="";
    echo '<p class="error">Please select a category</p>';
    }
    if($url && $sitetypeid){
    $q = "SELECT UserID, url, SiteID FROM sites WHERE url='$url' AND SiteTypeID='$sitetypeid' AND SiteTypeID!=$id";
    $query = mysqli_query ($dbc, $q);
    if (mysqli_num_rows($query) == 0){
    $q = "UPDATE sites SET url='$url', SiteTypeID='$sitetypeid' WHERE SiteID=$id LIMIT 1";
    $query = mysqli_query ($dbc, $q);
    if (mysqli_affected_rows($dbc) == 1){
    echo '<p><b>Your url has been successfully edited</b></p>';
    } elseif (mysqli_affected_rows($dbc) == 0){
    echo '<p>No new details have been inserted</p>';
    } else {
    echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
    echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
    }
    } else {
    echo '<p><b>That url has already been registered with that category, if you own this site you can change categories if you wish to. Please note that a website cannot be added multiple times in different categories.</b></p>';
    }
     
    }
    }
     
    ?>
    <h1>Edit Your Sites</h1><p><b>Do not add duplicate urls, they will be deleted. We only accept family-safe urls. We play fair and expect our members to do the same.</b> </p>
    <?php 
    $query2 = "SELECT s.SiteTypeID, u.UserID, s.SiteType FROM sitetypes AS s LEFT JOIN sites AS u USING (SiteTypeID) WHERE SiteID=$id";
    $mx = mysqli_query($dbc, $query2);
    if (mysqli_num_rows($mx) ==1){
    $row2 = mysqli_fetch_array($mx, MYSQLI_ASSOC);
    echo '<p>Your site\'s current category is <b>'.$row2['SiteType'] . '</b></p>';
    }
    ?>
    <?php
    $optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education', 
    18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping', 
    26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
    echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
    foreach ($optionarray as $key => $value){
    echo '<option value="$key"';
    if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == '. $key . ')) echo 'selected="selected">'; 
    echo $value . '</option>';
    }
    echo '</select>';
    ?>
    <?php
     
     
    $ms = "SELECT UserID, url, SiteID FROM sites WHERE SiteID=$id";
    $msp = mysqli_query($dbc, $ms);
    if (mysqli_num_rows($msp) > 0){
    $row = mysqli_fetch_array($msp, MYSQLI_ASSOC);
    echo '<p>Url:<input type="text" name="url" size="60" maxlength="80" value="' . $row['url'] .'" /><small>You can only add each url once into the database, each url must be unique.</small></p><p><input type="submit" name="submit" value="Edit Site Information!" /><input type="reset" name="reset" value="Clear Form" />
    <input type="hidden" name="id" value="' . $id . '" />
    </form><p><a href="delete_url.php?id=' . $row['SiteID'] . '"><b>Delete Url</b></a>';
    } else {
    echo '<p class="error">A system error occurred, we apologize for the inconvenience.</p>';
    }
    ?>
    <?php
    } else {
    $url = BASE_URL . 'index.php';
    header("Location: $url");
    }
    echo '</div>';
    include ('includes/footer.html');
    ?>
  8. The following code is from chapter 5 of this book. 

     

    if (!comments || !comments.value || (comments.value.indexOf('<') != -1) ){
    okay = false;
    alert ('Please enter your comments, without any HTML!');
     
    My question is why do we have to both put !comments and also !comments.value. Aren't they the same?? Why check comments if it doesn't have a value. I just learned php with Larry's book, and am new at javascript. 
  9. in the book it shows us how to drop an index by using the following

    sql statement. ALTER TABLE t DROP INDEX i

    i being the name of the index. I went into phpmyadmin and I don't see an option for removing an index unless there is a name attached to the index. If I would write an sql statement to drop the index how would I write it if the index doesn't have a name. 

  10. To solve the problem I had to add some code to my .htaccess file. The base url I am using to redirect users to my index.php page when they login is http://www.thisistheurl.com . When users type variations of this to get to my site before logging in, my login script doesn't work correctly on Chrome and Firefox. The code I had to add forces the url to change to www.thisistheurl.com even if someone gets to the website by typing in http://thisistheurl.com or thisistheurl.com. The code I had to add was

    #Force www:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^exampleurl.com [NC]
    RewriteRule ^(.*)$ http://www.exampleurl.com/$1 [L,R=301,NC]
     
    Without this code my login script doesn't work correctly. My login.php script is pretty much copied out of this book. I am hoping that this post is helpful to others who build a live site with the code that is provided in the book. Thanks to the Admin Administrators who helped me solve this, without your help I don't think I would of been able. thank you
  11. Hi, Yes the url changes after the login attempt. If I type thisistheurl.com in the browser and login the page jumps and redirects to www.thisistheurl.com with a successful login. I tried modifying the BASEURL to http://thisistheurl.com. And now if I type thisistheurl.com in the browser and login, it always works on the first attempt. But if I type www.thisistheurl.com it doesn't work. How can I fix this so it works either way. 

  12. I am including my header.html file, maybe the problem is in there. 

     

    <?php
    ob_start();
    session_start();
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <title><?php if (isset($page_title)) echo $page_title; ?></title>
    <meta name="description" content="<?php if (isset($description)){ echo $description; }?>">
    <meta name="keywords" content="<?php if (isset($keywords)){echo $keywords; }?>">
    <script type="text/javascript">
     
    $(document).ready(function(){
     
    $("#menu li").hover(function(){
    $(this).children(":hidden").slideDown();
     
    },function(){
    $(this).parent().find("ul").slideUp();
     
    });
     
    });
     
    </script>
    </head>
     
    <body>
    <div id="container">
    <div id="logo">
    <img src="images/invisible.jpg" border="0" alt="Invisible">
    </div>
    <div id="menu">
    <ul>
    <li><a href="#">About</a>
    <ul>
    <li class="sub"><a href="how_it_works.php">How It Works</a></li>
    <li class="sub"><a href="link_placement.php">Link Placement</a></li>
    <li class="sub"><a href="verifying_your_links.php">Verifying Links</a></li>
    <li class="sub"><a href="link_partners.php">Link Partners</a></li>
    <li class="sub"><a href="terms.php">Terms and Conditions</a></li>
    </ul>
    </li>
     
    <li><a href="#">Account Management</a>
    <ul>
     
    <?php
    if (isset($_SESSION['UserID'])){
                            echo '<li class="sub"><a href="logout.php">Logout</a></li>';
                            echo '<li class="sub"><a href="add_url.php?id=' . $_SESSION['UserID'] . '">Add Your Url</a></li>';
                            echo '<li class="sub"><a href="directory.php">Directory of Sites</a></li>';
                            echo '<li class="sub"><a href="view_sites.php?id=' . $_SESSION['UserID'] . '">View/Edit Urls</a></li>';
                            echo '<li class="sub"><a href="edit_account.php?id=' . $_SESSION['UserID'] . '">Edit Account</a></li>';
                            echo '<li class="sub"><a href="change_password.php">Change Password</a></li>';
                        } else {
                            echo '<li class="sub"><a href="login.php">Login</a></li>';
                            echo '<li class="sub"><a href="register.php">Register</a></li>';
                        } 
                        ?>
    <li class="sub"><a href="forgot_password.php">Forgot Password</a></li>
    </ul>
    </li>
    <li><a href="register.php">Register</a></li>
    <li><a href="contact.php">Contact Us</a></li>
     
     
    </ul>
    <div align="center" id="sidebar">
    <?php include('includes/ads.php'); ?>
    </div>
    </div>
  13. Hi

    Well you said to post the relevant part of my code, so that is why I only gave you the snippet of code. Here is my whole login.php page. The session_start() is in the header.html included file.

     

    <?php
    include ('includes/header.html');
    include ('includes/config.inc.php');
    $page_title = 'Login';
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    require (MYSQL);
    $trimmed = array_map('trim', $_POST);
    if (!empty($trimmed['Email']) && filter_var($trimmed['Email'], FILTER_VALIDATE_EMAIL, FILTER_SANITIZE_EMAIL)){
    $e = mysqli_real_escape_string($dbc, $_POST['Email']);
    } else {
    $e = FALSE;
    echo '<p class="error">You forgot to enter your email address, or the email you entered is invalid.</p>';
    }
    if (!empty($trimmed['Pass']) && (preg_match ('/^\w{4,20}$/', $trimmed['Pass']))){
    $p = mysqli_real_escape_string($dbc, $_POST['Pass']);
    } else {
    $p = FALSE;
    echo '<p class="error">You forgot to enter your password, or the password you entered is invalid.</p>';
    }
    if ($e && $p){
    $q = "SELECT UserID, Fname FROM users WHERE (Email='$e' AND Pass=SHA1('$p')) AND Active IS NULL";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    if (@mysqli_num_rows($r) == 1){
    $_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
    mysqli_free_result($r);
    mysqli_close($dbc);
    $url = BASE_URL . 'index.php';
    ob_end_clean();
    header("Location: $url");
    exit();
    } else {
    echo '<p class="error">Either the email address and password entered do not match those on file or you have not yet activated your account.</p>';
    }
    } else {
    echo '<p class="error">Please try again.</p>';
    }
    mysqli_close($dbc);
    } //end of submit conditional
     
     
    ?>
    <div class="text">
    <h1>Login</h1>
    <p>Your browser must allow cookies in order to log in.</p>
    <form action="login.php" method="post">
    <fieldset>
    <p><b>Email: <input type="text" name="Email" size="20" maxlength="60" value="<?php if(isset($trimmed['Email']))echo $trimmed['Email']; ?>"/></b></p>
    <p><b>Password: <input type="password" name="Pass" size="20" maxlength="20" value="<?php if(isset($trimmed['Email']))echo $trimmed['Email']; ?>"/></b></p>
    <input type="submit" name="submit" value="Login!" />
    </fieldset>
    </form>
    </div>
    <? include ('includes/footer.html'); ?>
  14. Here is the relevant part of my code like you asked. 

     

    if ($e && $p){
    $q = "SELECT UserID, Fname FROM users WHERE (Email='$e' AND Pass=SHA1('$p')) AND Active IS NULL";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    if (@mysqli_num_rows($r) == 1){
    $_SESSION = mysqli_fetch_array($r, MYSQLI_ASSOC);
    mysqli_free_result($r);
    mysqli_close($dbc);
    $url = BASE_URL . 'index.php';
    ob_end_clean();
    header("Location: $url");
    exit();
  15. Yes I ran the queries directly in phpmyadmin, in the SQL query box I typed out the query several times in a row and each time it returns the userid like it should. Yes I turned error reporting on. I also put echo statements on my index page to see if the $_session variable was set, it doesn't get set the first time around, it takes on the second or third attempt. I have tried numerous things like flushing my dns, and clearing my cache and cookies. Those are the steps I have taken, I don't know what else to do. I have sincerely been trying VERY hard to make this code work, I did my best to do what you and Larry suggested, but I am a beginner. 

  16. Here is the problem I have been having. I have tried numerous things to resolve it. Nothing seems to work. 

     

    I have my login script from this book and I put it on a live domain to test it. And I can login fine with Internet explorer and Safari. But with Chrome and Firefox. It always fails to log me in on the first attempt, it takes 2 or sometimes 3 attempts. I am thinking of using the code from this book to build a website but I am completely clueless as to why it is doing this. I have triple-checked my code and it is like in the book. Could it be my SQL table that has a problem.

     

    I tested the query in phpmyadmin in the SQL form, and it always returns the user_id. 

     

    When I try to login the page kind of jumps and it redirects me to index.php without logging me in. 

    I have been at this for 3 days, any help would be HIGHLY appreciated. 

  17. Hi, exactly where do I put echo lines, on my login.php page? if it is on login.php, where on the page do I put them? And what do I echo out, would it be $_SESSION['UserID']

     

    I just put an echo $_SESSION['UserID'] on my index.php page and tried to login. It gave me an error message saying UserID undefined index. So it looks like my session variable isn't getting set, the first time I try to login, it always takes about 2 or 3 attempts before I login successfully. What can I do to correct this. 

     

    I checked on all my pages and the session_start() is at the top of every page except for the $page_title variable before it.

×
×
  • Create New...