Hi,
I’m been trying to implement the following tutorial:
http://www.olate.co.uk/articles/185
But I don’t understand where you put the init file in each page.
He says:
The next part just inserts the data into the database. You may notice that we don’t have a mysql_connect() function anywhere. This is because I will leave it up to a initialisation file which will be included on every page. An example file would look like the following:
EG the register form is:
<?php
// Include init file
include 'init.php';
if (!isset($_POST['submit']))
{
// Show the form
include 'register_form.inc.php';
exit;
}
else
{
// Check if any of the fields are missing
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']))
{
// Reshow the form with an error
$reg_error = 'One or more fields missing';
include 'register_form.inc.php';
exit;
}
// Check if the passwords match
if ($_POST['password'] != $_POST['confirmpass'])
{
// Reshow the form with an error
$reg_error = 'Your passwords do not match';
include 'register_form.inc.php';
exit;
}
// Everything is ok, register
user_register ($_POST['username'], $_POST['password']);
echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';
}
?>
So do you go:
<?php
// Start the session
session_start();
// MySQL Settings
$db_host = 'localhost';
$db_user = 'matt';
$db_pass = '1234';
$db_database = 'user_auth';
// Connect to the database
mysql_connect ($db_host, $db_user, $db_pass) or die ('Could not connect to the database.');
mysql_selectdb ($db_database) or die ('Could not select database.');
// Seed the random number generator
srand();
// Include functions
include 'functions.php';
?>
<?php
// Include init file
include 'init.php';
if (!isset($_POST['submit']))
{
// Show the form
include 'register_form.inc.php';
exit;
}
else
{
// Check if any of the fields are missing
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirmpass']))
{
// Reshow the form with an error
$reg_error = 'One or more fields missing';
include 'register_form.inc.php';
exit;
}
// Check if the passwords match
if ($_POST['password'] != $_POST['confirmpass'])
{
// Reshow the form with an error
$reg_error = 'Your passwords do not match';
include 'register_form.inc.php';
exit;
}
// Everything is ok, register
user_register ($_POST['username'], $_POST['password']);
echo 'Thank you for registering on our site, <a href="index.php">click here</a> to go back.';
}
?>
I can’t get the links or I guess syntax right (my usual problem!)
Help appreciated , Thanks