Building Practical PHP and Mysql Project: Course: Z1167 Advanced in Web Based Application Development Year: 2019
Building Practical PHP and Mysql Project: Course: Z1167 Advanced in Web Based Application Development Year: 2019
Application Development
Year : 2019
Session 11
Building Practical PHP and MySQL Project
Syntax error
If a statement does not follow the rules of a language.
Syntax errors are often also called parser errors when
discussing interpreted languages, such as PHP, or
compiler errors when discussing compiled languages,
such as C or Java.
This error was produced by the following script:
<?php
$date = date(m.d.y’);
?>
Runtime errors
• Runtime errors are not caused solely by the contents
of your script. They can rely on interactions between
your scripts and other events or conditions.
• The statement require (‘filename.php’);
is a perfectly valid PHP statement. It contains no
syntax errors.
• This statement might, however, generate a runtime
error. If you execute this statement and filename.php
does not exist or the user who the script runs as is
denied read permission, you will get an error
resembling this one:
Logic errors
• Logic errors can be the hardest type of error to find
and eliminate.
• This type of error occurs when perfectly valid code
does exactly what it is instructed to do, but that was
not what the writer intended.
• Logic errors can be caused by a simple typing error,
such as
for ( $i = 0; $i < 10; $i++ );
{
echo ‘doing something<br />’;
}
11.4 Registering
Several alternatives can be used for user authentication
1. Users should be able to register their usernames and
passwords.
2. You need some restrictions on the length and format
of each username and password.
3. You should store passwords in an encrypted format
for security reasons.
4. Users should be able to log in with the details they
supplied in the registration process.
5. Users should be able to change their passwords as an
aid to security.
Login
• If users type their details into the form at login.php and
submit it,they will be taken to the script called
member.php
Function login($username, $password)
// check username and password with db
// if yes, return true
// else throw exception
{
// connect to db
$conn = db_connect();
// check if username is unique
$result = $conn->query(“select * from user
where username=’$username’
and passwd = sha1(‘$password’)”);
if (!$result)
throw new Exception(‘Could not log you in.’);
if ($result->num_rows>0)
return true;
else
throw new Exception(‘Could not log you in.’);
Bina Nusantara University } 16
function check_valid_user()
// see if somebody is logged in and notify them if not
{
if (isset($_SESSION[‘valid_user’]))
{
echo ‘Logged in as ‘.stripslashes($_SESSION[‘valid_user’]).’.’;
echo ‘<br />’;
}
else
{
// they are not logged in
do_html_heading(‘Problem:’);
echo ‘You are not logged in.<br />’;
do_html_url(‘login.php’, ‘Login’);
do_html_footer();
exit;
}
}
Logging Out
<?php
// include function files for this application
require_once(‘bookmark_fns.php’);
session_start();
$old_user = $_SESSION[‘valid_user’];
// store to test if they *were* logged in
unset($_SESSION[‘valid_user’]);
$result_dest = session_destroy();
Changing Passwords
$conn = db_connect();
$result = $conn->query( “update user
set passwd = sha1(‘$new_password’)
where username = ‘$username’”);
if (!$result)
throw new Exception(‘Password could not be changed.’);
else
return true; // changed successfully
if($new)
{
//new item selected
if(!isset($_SESSION[‘cart’]))
{
$_SESSION[‘cart’] = array();
$_SESSION[‘items’] = 0;
$_SESSION[‘total_price’] =’0.00’;
}
Using PHP and MySQL for large project: Planning and Running
a Web Application Project