Jump to content
Larry Ullman's Book Forums

Recommended Posts

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, '/\\');

Share this post


Link to post
Share on other sites

Server names are case insensitive, directory names are normally case sensitive. Assuming all of your files are in the tuition directory, the URL would be http://localhost/tuition.

 

You're using $_localhost['HTTP_HOST']. I assume there's no such thing as $_localhost (it'd be new to me). Perhaps you meant $_SERVER['HTTP_HOST']?

Share this post


Link to post
Share on other sites

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

?>

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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');
	?>

Share this post


Link to post
Share on other sites

These are just basic URL/file system problems. Here, I assume you don't have a D:\xampp\htdocs\tuition\includes\login_functions.inc.php file, which is why you're getting that error. (Also, FYI, info about the file system would be more helpful debugging here as the error is a lack of ability to include a file; the specific code in all the scripts doesn't illuminate the problem.)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...