Jump to content
Larry Ullman's Book Forums

Paul_Blackpool

Members
  • Posts

    68
  • Joined

  • Last visited

Paul_Blackpool's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. Thanks Larry I've managed to fix it. For ref if anyone else is having the same problem: This bit // URL is http:// plus the host name plus the current directory $url = 'http://localhost/tuition'; Should be: // URL is http:// plus the host name plus the current directory: $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); It all works fine now. I couldn't see the wood for the trees as we say in the UK
  2. Sorry to be a pain Larry I've followed all the instruction to the letter and the error I am getting now is Warning: require_once(includes/login_functions.inc.php): failed to open stream: No such file or directory in D:\xampp\htdocs\tuition\loggedin.php on line 9 Fatal error: require_once(): Failed opening required 'includes/login_functions.inc.php' (include_path='.;D:\xampp\php\PEAR') in D:\xampp\htdocs\tuition\loggedin.php on line 9 The login pages are <?php # Script 11.1 - login_page_inc.php // This page prints any errors associated with logging in. // and it creates the entire login page including the form. // Include the header: $page_title = 'Login'; include ('header.html'); // Print any error messages, if they excist: if (!empty($_errors)) { echo '<h1>Error!</h1> <p class="error">The following error(s) occured:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; } // Display the form ?> <h1>Login</h1> <form action="loggedin.php" method="post"> <p>Email Address: <input type="text" name="email" size="20" maxlengh="80" /> <p>Password: <input type="password" name="pass"size="20" maxlengh="20" /></p> <p><input type="submit" name="submit" value="Login" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer include ('footer.html'); ?> <?php # Script 11.2 - login_functions_inc.php // This page defines 2 functions used by the login/logout process /* This function determins and returns an absoloute URL * It takes one argument: the page that includes the URL. * the argument defaults to index.php */ function absoluite_url ($page = 'index.php') { // Start defining the URL // URL is http:// plus the host name plus the current directory $url = 'http://localhost/tuition'; // Remove any trailing slashes: $url = rtrim($url, '/\\'); // Add the page $url .= '/' . $page; // Return the URL: return $url; } // End of absoluite_url() function /* This function validates the form data (the email address and password) * If both are present the database is queried * The function requires a database connection * The function returns an array of information, including: * -a TRUE/FALSE variable indicating success. 9 An array of eithers errors or the database result. */ function check_login($dbc, $email = '', $pass = '') { $errors = array(); // Initiolise the error array. // validate the email address: if (empty($email)) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($email)); } // validate the password: if (empty($pass)) { $errors[] = 'You forgot to enter your password.'; } else { $p = mysqli_real_escape_string($dbc, trim($pass)); } if (empty($errors)) { // If everythings OK. // Retreive the user_id and first_name for that email/password combination. $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // run the query. // Check the result if (mysqli_num_rows($r) == 1) { // Fetch the record $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); // Return true and the record. return array(true, $row); } else { // Not a match. $errors[] = 'The email address and password entered do not match those on file'; } } // end of empty(errors0 IF // Return faulse and errors: return array(false, $errors); } // End of check_login() function ?> <?php // Script 11.3 - login.php // This page processes the login form submission. // Upon successfull login the user is re-directed. // Two include files are required // Send NOTHING to the web browser prior to the setcookie() lines! // Check if the form has been submitted if(isset($_POST['submitted'])) { // For processing the login require_once ('includes/login_functions_inc.php'); // Connect to the database require_once ('conn.php'); // check the login: list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']); if ($check) { // OK! // Set the cookies: setcookie ('user_id', $data['user_id']); setcookie ('first_name', $data['first_name']); // Redirect $url = absolute_url ('loggedin.php'); header("Location: $url"); exit(); // quite the script. } else { // If unsuccessfull // Assign $ data to $ errors for error reporting. // in the login_page.inc.php file $errors = $data; } mysqli_close($dbc); // Close the database connection } // End of main submit conditional // Create the page include ('includes/login_page_inc.php'); ?> <?php # Script 11.4 - loggedin.php // the user is re-directed here from login.php // If no cookie is present if (!isset($_COOKIE['user_id'])) { // need the functions to create an absoloute URL: require_once ('includes/login_functions.inc.php'); $url = absolute_url(); header("Location: $url"); exit(); // Quit the script. } // Set the page title and include the HTML Header. $page_title = 'Logged In!'; include ('includes/header.html'); // Print the customised message: echo "<h1>Logged In!</h1> <p>You are now logged in, {$_COOKIE['first_name']}!</p> <p><a href=\"logout.php\">Logout</a></p>"; include ('includes/footer.html'); ?>
  3. Thanks for the reply Larry. The URL is: http://localhost/login.php Hope this helps Also The login.php page is located at C:/xampp/htdocs/tuition The login_page_inc.php and login_functions_inc.php are located at C:/xampp/htdocs/tuition/includes - in the same includes directory as the header and footer. Thanks
  4. Sorry Larry i forgot to include the login.php. <?php // Script 11.3 - login.php // This page processes the login form submission. // Upon successfull login the user is re-directed. // Two include files are required // Send NOTHING to the web browser prior to the setcookie() lines! // Check if the form has been submitted if(isset($_POST['submitted'])) { // For processing the login require_once ('includes/login_functions_inc.php'); // Connect to the database require_once ('conn.php'); // check the login: list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']); if ($check) { // OK! // Set the cookies: setcookie ('user_id', $data['user_id']); setcookie ('first_name', $data['first_name']); // Redirect $url = absolute_url ('loggedin.php'); header("Location: $url"); exit(); // quite the script. } else { // If unsuccessfull // Assign $ data to $ errors for error reporting. // in the login_page.inc.php file $errors = $data; } mysqli_close($dbc); // Close the database connection } // End of main submit conditional // Create the page include ('includes/login_page_inc.php'); ?> If I try and login i now get the error Object not found!The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
  5. Thanks Larry. I must be doing something wrong here. I have posted the login page and the login script. If i try and login using incorrect credentials i don't get any error messages and if i enter the correct credentials i get no indication i am logged in. Both the login_page.inc.php and the login_functions.inc.php are both in the includes folder as the book tells me to. <?php # Script 11.1 - login_page_inc.php // This page prints any errors associated with logging in. // and it creates the entire login page including the form. // Include the header: $page_title = 'Login'; include ('header.html'); // Print any error messages, if they exist: if (!empty($_errors)) { echo '<h1>Error!</h1> <p class="error">The following error(s) occured:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p>'; } // Display the form ?> <h1>Login</h1> <form action="../login.php" method="post"> <p>Email Address: <input type="text" name="email" size="20" maxlengh="80" /> <p>Password: <input type="password" name="pass"size="20" maxlengh="20" /></p> <p><input type="submit" name="submit" value="Login" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer include ('footer.html'); ?> <?php # Script 11.2 - login_functions_inc.php // This page defines 2 functions used by the login/logout process /* This function determins and returns an absoloute URL * It takes one argument: the page that includes the URL. * the argument defaults to index.php */ function absoluite_url ($page = 'index.php') { // Start defining the URL // URL is http:// plus the host name plus the current directory $url = 'http//localhost/tuition'; // Remove any trailing slashes: $url = rtrim($url, '/\\'); // Add the page $url .= '/' . $page; // Return the URL: return $url; } // End of absoluite_url() function /* This function validates the form data (the email address and password) * If both are present the database is queried * The function requires a database connection * The function returns an array of information, including: * -a TRUE/FALS variable indicating success. 9 An array of eithers errors or the database result. */ function check_login($dbc, $email = '', $pass = '') { $errors = array(); // Initiolise the error array. // validate the email address: if (empty($email)) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($email)); } // validate the password: if (empty($pass)) { $errors[] = 'You forgot to enter yourpassword.'; } else { $p = mysqli_real_escape_string($dbc, trim($pass)); } if (empty($errors)) { // If everythings OK. // Retreive the user_id and first_name for that email/password combination. $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // run the query. // Check the result if (mysqli_num_rows($r) == 1) { // Fetch the record $row = mysqli_fetch_array ($r, MYSQLI_ASSOC); // Return true and the record. return array(true, $row); }else { // Not a match. $errors[] = ' The email address and password entered do not match those on file'; } } // end of empty(errors) IF // Return faulse and errors: return array(false, $errors); } // End of check_login() function ?>
  6. Sorry to be a pain but I'm a little confused where to enter the server and directory name, also if they should be in upper or lower case. I'm using xampp on localhost and the directory path is D://xampp/htdocs/tuition. If someone could point me in the right direction. I've already tried a number of computations but I must be doing something wrong, i cant seem to get it to work. // Start defining the URL // URL is http:// plus the host name plus the current directory $url = 'http//' . $_localhost['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); // Remove any trailing slashes: $url = rtrim($url, '/\\');
  7. I am having a problem when I click on the link that this script creates. My structure is D://xampp/uploads (This is where the images have been uploaded to) My main files are in D://xampp/htdocs/tuition. When I publish the script in the browser it sees the images in the table I.E.: Image Name - Image Size basketball.jpg 154kb penguins.jpg 89kb I have checked and double checked the code. I have also tried replacing line 46 from $dir = '../../uploads'; // Define the directory to view. to $dir = 'D://xampp/uploads'; // Define the directory to view. But i still get the same error: The page code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Images</title> <script language="JavaScript"> <!-- // Hide from old browsers. // Make a pop-up window function: function create_window (image, width, height) { // Add some pixels to the width and height: width = width + 10; height = height + 10; // If the window is already open, // resize it to the new dimensions: if (window.popup && !window.popup.closed) { window.popup.resizeTo(width, height); } // Set the window properties: var specs = "location=no, scrollbars=no, menubars=no, toolbars=no, resizable=yes, left=0, top=0, width=" + width + ", height=" + height; // Set the URL: var url = "show_image.php?image=" + image; // Create the pop-up window: popup = window.open(url, "ImageWindow", specs); popup.focus(); } // End of function. //--></script> </head> <body> <p>Click on an image to view it in a separate window.</p> <table align="center" cellspacing="5" cellpadding="5" border="1"> <tr> <td align="center"><b>Image Name</b></td> <td align="center"><b>Image Size</b></td> </tr> <?php # Script 10.4 - images.php // This script lists the images in the uploads directory. $dir = '../../uploads'; // Define the directory to view. $files = scandir($dir); // Read all the images into an array. // Display each image caption as a link to the JavaScript function: foreach ($files as $image) { if (substr($image, 0, 1) != '.') { // Ignore anything starting with a period. // Get the image's size in pixels: $image_size = getimagesize ("$dir/$image"); // Calculate the image's size in kilobytes: $file_size = round ( (filesize ("$dir/$image")) / 1024) . "kb"; // Make the image's name URL-safe: $image = urlencode($image); // Print the information: echo "\t<tr> \t\t<td><a href=\"javascript:create_window('$image',$image_size[0],$image_size[1])\">$image</a></td> \t\t<td>$file_size</td> \t</tr>\n"; } // End of the IF. } // End of the foreach loop. ?> </table> </body> </html> However when I click in the link to show the image in a popup window I get the error: Object not found!The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster. Error 404 localhost Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
  8. Just got a little stuck. Iv'e created a form but I cant get the items from my multi select items list into the database. Everything else works fine could just do with a little help. PHP <?php # script 8.5 - register.php # 2 $page_title = 'Register'; include ('../Includes/header.html'); // Check if the form has been submitted: if (isset($_POST['submitted'])) { require_once ('../conn/mysqli_connect.php'); // connect to the database. $errors = array(); // Initialise error array // Check for a first name: if (empty($_POST['first_name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = mysqli_real_escape_string($dbc,trim($_POST['first_name'])); } // Check for last name: if (empty($_POST['last_name'])) { $errors[] = 'You forgot to enter your last name.'; } else { $ln = mysqli_real_escape_string($dbc,trim($_POST['last_name'])); } // Check for an email address: if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email address.'; } else { $e = mysqli_real_escape_string($dbc,trim($_POST['email'])); } // Check for a password and match against the confirmed password: if (!empty($_POST['pass1'])) { if ($_POST['pass1'] != $_POST['pass2']) { $errors[] = 'Your password did not match your confirm password.'; } else { $p = mysqli_real_escape_string($dbc,trim($_POST['pass1'])); } } else { $errors[] = 'You forgot to enter your password.'; } if (empty($errors)) { // If everythings OK: // Register the user into the database. // Make the query: $q = "INSERT INTO users (first_name, last_name, email, pass, items_list, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )"; $r = mysqli_query ($dbc, $q); // Run the query: if ($r) { // if it ran OK. // Print the message echo '<h1>Thank You</h1> <p>You are now registered. In chapter 11 you will actually be able to login!</p><p><br /></p>'; } else { // If it did not run OK // message echo '<h1>System Error</h1> <p class ="error">You could not be registered due to a system error. Please try in a little while. We appologise for any inconvenience,</p>'; // Debug message: echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } // end of the if ($r) IF mysqli_close($dbc); // Close the database connection. // include the footer and quit the script. include ('../Includes/3.4_3.4_footer.html'); exit(); } else { // Report the errors. echo '<h1>Error!</h1> <p class="error">The following error(s) occured:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF myli_close($dbc); // Close the database. } // End of mail submitt. ?> HTML Form <style type="text/css"> #apDiv1 { position:absolute; width:200px; height:115px; z-index:1; left: 107px; top: 229px; } </style> <h1>Register</h1> <form action="register_2.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlengh="20" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p> <p>Last name: <input type="text" name="last_name" size="15" maxlengh="24" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlengh="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p> <p>Password: <input type="password" name="pass1" size="10" maxlengh="20" /></p> <div id="apDiv1"></div> <p>Confirm Password: <input type="password" name="pass2" size="10" maxlengh="20" /></p> <P> List: <select name="items_list[]" multiple size="1" > <option>Item 1</option> <option>Item 2</option> <option>Item 3</option> </select> <p><input type="submit" name="submit" value="register" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php include ('../Includes/3.4_3.4_footer.html'); ?>
  9. I have a multi select combo box on my site. If the user fills in the form wrong the site retains all the information already entered, however the multi select combo box comes back with the selections de-selected. How can I get the combo box to retain the values entered when the user happens to fill something wrong elsewhere on the form? <select name="Skills[]" multiple size="1" id="Combobox1"> <option value="LGV Class 1">LGV Class 1</option> <option value="LGV Class 2">LGV Class 2</option> <option value="7.5 Ton Drivers">7.5 Ton Drivers</option> <option value="Non LGV Driver">Non LGV Driver</option> <option value="Fork Lift Driver">Fork Lift Driver</option> <option value="Garage Staff">Garage Staff</option> <option value="Transport Manager">Transport Manager</option> <option value="Traffic Planner">Traffic Planner</option> </select>
  10. I am getting to the end of Edition 3 and thinking about Edition 4. What’s the deference or whats new in Edition 4?
  11. Whe I try to upload a file (Chapter 10 file_upload.php) I get the following errors Warning: move_uploaded_file(../tmp/agriculture.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\trsb\upload_image.php on line 28 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpA805.tmp' to '../tmp/agriculture.jpg' in C:\xampp\htdocs\trsb\upload_image.php on line 28 The folder I have created is called "tmp" and I followed the instructions for the local server. here is the line the error is referring to: // Move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../tmp/{$_FILES['upload']['name']}")) { echo '<p><em>The file has been uploaded</em></p>'; } // End of move . IF.
  12. Thanks Larry I started Mercury in the xammp control panel and it worked fine
  13. Thanks Larry and Jonathon. I followed the link and also looked at other sites for a soloution and they all tell me to do the same thing but it's still not working. I have posted the relavent bit of the php.ini and also the page code. Don't think the code is wrong as the form all works correctly. [mail function] XAMPP: Comment out this if you want to work with an SMTP Server like Mercury SMTP = mail.me.co.uk sendmail_from = paul@me.co.uk <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Contact Me</title> </head> <body> <h1>Contact Us</h1> <?php # Script 10.1 = email.php include ('includes/header.html'); // Check for the form submission if (isset($_POST['submitted'])) { // Minimal form validation if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']) ) { // Create the body $body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}"; // make it longer than 70 caracters long: $body = wordwrap($body, 70); // Send the email mail('paul@me.co.uk', 'Contact Form Submission', $body, "Form: {$_POST['email']}"); //Print the message: echo '<p style="font-weight: bold; color: red">Thank you for contacting us. We will reply some day,</em></p>'; // Clear $_POST (So that the form's not sticky): $_POST = array(); } else { echo '<p style="font-weight: bold; Color: #C00">Please go back and fill out the form correctly.</p>'; } } // End of mail isset() IF. // Now create the HTML form ?> <p>Please use this form to contact us.</p> <form action="email.php" method="post"> <p>Name: <input type="text" name="name" size="30" maxlengh="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p> <p>Email Address: <input type="text" name="email" size="30" maxlengh="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p> <p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p> <p><input type="submit" name="submit" value="send!" /></p> <input type="hidden" name="submitted" value="true" /> </form> <?php include ('includes/footer.html'); ?> </body> </html> ; smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = paul@wcrltd.co.uk
×
×
  • Create New...