Jump to content
Larry Ullman's Book Forums

azdrian

Members
  • Posts

    3
  • Joined

  • Last visited

azdrian's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. hello all i am building a small forum website for kids to discuss their school work etc. As this website will be assess by my prospect employer for a job as a entry level web application developer. using part of your script use in chapter 09. But the error am having when i tried to display the username of the user who just login from the session it 's not showing am not getting any error showing but the username just not showing. thanks in advance. here's the login script: <?php // login.php if (isset($_POST['submitted'])) { require_once ('includes/login_functions.inc.php'); require_once ('mysqli_connect.php'); list ($check, $data) = check_login($dbc, $_POST['user_name'] , $_POST['user_pass']); if ($check) { // OK! // Set the session data:. session_start(); $_SESSION['user_id'] = $data['user_id']; $_SESSION['user_name'] = $data['user_name']; // Store the HTTP_USER_AGENT: $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); // Redirect: $url = absolute_url ('loggedin.php'); header("Location: $url"); exit(); } else { // Unsuccessful! $errors = $data; } mysqli_close($dbc); } // End of the main submit conditional. include ('includes/login_page.inc.php'); ?> here's the loggedin script: <?php // loggedin.php session_start(); // The user is redirected here from login.php. // Start the session. // If no session value is present, redirect the user: // Also validate the HTTP_USER_AGENT! if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']) )) { require_once ('includes/login_functions.inc.php'); $url = absolute_url(); header("Location: $url"); exit(); } $page_title = 'Logged In!'; include ('includes/header.html'); // Print a customized message: echo "<h1>Logged In!</h1> <p>You are now logged in, {$_SESSION['user_name']}!</p> <p><a href=\"logout.php\">Logout</a></p>"; include ('includes/footer.html'); ?> here' the login funtion: function check_login($dbc, $user_name = ' ', $user_pass = ' ') { $errors = array(); // Initialize error array. // Validate the email address: if (empty($user_name)) { $errors[] = 'You forgot to enter your username.'; } else { $u = mysqli_real_escape_string($dbc, trim($user_name)); } // Validate the password: if (empty($user_pass)) { $errors[] = 'You forgot to enter your password.'; } else { $p = mysqli_real_escape_string($dbc, trim($user_pass)); } if (empty($errors)) { // If everything's OK. // Retrieve the user_id and first_name for that email/password combination: $q = "SELECT user_id, user_email FROM users WHERE user_name = '$u' AND user_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 username and password entered do not match those on file.'; } } // End of empty($errors) IF. // Return false and the errors: return array(false, $errors); } // End of check_login() function. here's the login_page.inc.php: <?php //login_Page.inc.php $page_title ='Login'; include('includes/header.html');if (!empty($errors)) { echo '<h1>Error!</h1><p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { echo " - $msg<br />\n"; } echo '</p><p>Please try again. </p>'; } ?> <h1>Login Page</h1><!-- Start of FORM --> <form method="post" action="login.php"> <p>Username: <input type="text" name="user_name" size="20" maxlength="80" value=" <?php if (isset($_POST['user_name'])) {echo $_POST['user_name'];} ?> "></p> <p>Password: <input type="password" name="user_pass" size="20" maxlength="20"></p> <p><input type="submit" name="submit" value="login"></p> <input type="hidden" name="submitted" value="TRUE"> </form> <!-- End of FORM --> <?php include('includes/footer.html'); ?>
  2. hello, Going through the example application "my site of quotes" in the book. while testing the application i experience a problem when i add a new quote on the add_quote.php page. the quote is added to the database but this is the error message i got: Fatal error: Cannot redeclare is_administrator() (previously declared in C:\wamp\www\includes\functions.php:7) in C:\wamp\www\includes\functions.php on line 16 here the add_quote page code: <?php // Define a page title and include the header: define('TITLE', 'Add a Quote'); include('templates/header.html'); print '<h2>Add 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(); } // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. if ( !empty($_POST['quote']) && !empty($_POST['source']) ) { // Need the database connection: include('../mysql_connect.php'); // Prepare the values for storing: $quote = mysql_real_escape_string(trim(strip_tags($_POST['quote'])), $dbc); $source = mysql_real_escape_string(trim(strip_tags($_POST['source'])), $dbc); // Create the "favorite" value: if (isset($_POST['favorite'])) { $favorite = 1; } else { $favorite = 0; } $query = "INSERT INTO quotes (quote, source, favorite) VALUES ('$quote', '$source', $favorite)"; $result = mysql_query($query, $dbc); if (mysql_affected_rows($dbc) == 1){ // Print a message: print '<p>Your quotation has been stored.</p>'; } else { print '<p class="error">Could not store the quote because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>'; } // Close the connection: mysql_close($dbc); } else { // Failed to enter a quotation. print '<p class="error">Please enter a quotation and a source!</p>'; } } // End of submitted IF. // Leave PHP and display the form: ?> <form action="add_quote.php" method="post"> <p><label>Quote <textarea name="quote" rows="5" cols="30"></textarea></label></p> <p><label>Source <input type="text" name="source" /></label></p> <p><label>Is this a favorite? <input type="checkbox" name="favorite" value="yes" /></label></p> <p><input type="submit" name="submit" value="Add This Quote!" /></p> </form> <?php include('templates/header.html'); ?>
×
×
  • Create New...