Scusa ma ieri sera avevo dei problemi col forum e non mi faceva rispondere al post..!! Ti metto i file necessari, non ho tempo di provarli ma spero che siano funzionanti con le modifiche che ho fatto...
File install.php (crea le tabelle necessarie all'interno del DB) (Scusa ma se lo metto tra i tag php non mi permette di risponderti, non so perché solo con questo file)
<?php
require 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
// crea la tabella utenti
$query = 'CREATE TABLE IF NOT EXISTS site_user (
user_id INTEGER NOT NULL AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password CHAR(41) NOT NULL,
PRIMARY KEY (user_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
// crea la tabella informazioni-utenti
$query = 'CREATE TABLE IF NOT EXISTS site_user_info (
user_id INTEGER NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
city VARCHAR(20),
state CHAR(2),
hobbies VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES site_user(user_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
// popola la tabella utenti
$query = 'INSERT IGNORE INTO site_user
(user_id, username, password)
VALUES
(1, "john", PASSWORD("secret")),
(2, "sally", PASSWORD("password"))';
mysql_query($query, $db) or die (mysql_error($db));
// popola la tabella informazioni-utenti
$query = 'INSERT IGNORE INTO site_user_info
(user_id, first_name, last_name, email, city, state, hobbies)
VALUES
(1, "John", "Doe", "jdoe@example.com", NULL, NULL, NULL),
(2, "Sally", "Smith", "ssmith@example.com", NULL, NULL, NULL)';
mysql_query($query, $db) or die (mysql_error($db));
echo 'Success!';
?>
File db.inc.php (i parametri per la connessione al database)
Codice PHP:
<?php
define('MYSQL_HOST','localhost');
define('MYSQL_USER','root');
define('MYSQL_PASSWORD','');
define('MYSQL_DB','nomedeltuodatabase');
?>
File index.php
Codice PHP:
<?php
session_start();
?>
<html>
<head>
<title>Logged In</title>
</head>
<body>
<h1>Welcome to the home page!</h1>
<?php
if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) {
?>
Thank you for logging into our system, [b]<?php
echo $_SESSION['username'];?>.[/b]</p>
You may now [url="user_personal.php"]click here[/url] to go to your
own personal information area and update or remove your information should
you wish to do so.</p>
<?php
} else {
?>
You are currently not logged in to our system. Once you log in,
you will have access to your personal area along with other user
information.</p>
If you have already registered, <a href="login.php">click
here</a> to log in. Or if you would like to create an account,
[url="register.php"]click here[/url] to register.</p>
<?php
}
?>
File login.php
Codice PHP:
<?php
session_start();
include 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
// filtro i valori in ingresso
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$redirect = (isset($_REQUEST['redirect'])) ? $_REQUEST['redirect'] : 'main.php';
if (isset($_POST['submit'])) {
$query = 'SELECT admin_level FROM site_user WHERE ' .
'username = "' . mysql_real_escape_string($username, $db) . '" AND ' .
'password = PASSWORD("' . mysql_real_escape_string($password, $db) . '")';
$result = mysql_query($query, $db) or die(mysql_error($db));
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$_SESSION['username'] = $username;
$_SESSION['logged'] = 1;
$_SESSION['admin_level'] = $row['admin_level'];
header ('Refresh: 5; URL=' . $redirect);
echo '
You will be redirected to your original page request.</p>';
echo '
If your browser doesn\'t redirect you properly automatically, ' .
'[url="' . $redirect . '"]click here[/url].</p>';
mysql_free_result($result);
mysql_close($db);
die();
} else {
// set these explicitly just to make sure
$_SESSION['username'] = '';
$_SESSION['logged'] = 0;
$error = '
[b]You have supplied an invalid username and/or ' .
'password![/b] Please <a href="register.php">click here ' .
'to register</a> if you have not done so already.</p>';
}
mysql_free_result($result);
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php
if (isset($error)) {
echo $error;
}
?>
<form action="login.php" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="20" size="20"
value="<?php echo $username; ?>"/></td>
</tr><tr>
<td>Password:</td>
<td><input type="password" name="password" maxlength="20" size="20"
value="<?php echo $password; ?>"/></td>
</tr><tr>
<td> </td>
<td>
<input type="hidden" name="redirect" value="<?php echo $redirect ?>"/>
<input type="submit" name="submit" value="Login"/>
</tr>
</table>
</form>
</body>
</html>
<?php
mysql_close($db);
?>
File register.php
Codice PHP:
<?php
session_start();
include 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
$hobbies_list = array('Computers', 'Dancing', 'Exercise', 'Flying', 'Golfing',
'Hunting', 'Internet', 'Reading', 'Traveling', 'Other than listed');
// filtro i valori in ingresso
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$first_name = (isset($_POST['first_name'])) ? trim($_POST['first_name']) : '';
$last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : '';
$email = (isset($_POST['email'])) ? trim($_POST['email']) : '';
$city = (isset($_POST['city'])) ? trim($_POST['city']) : '';
$state = (isset($_POST['state'])) ? trim($_POST['state']) : '';
$hobbies = (isset($_POST['hobbies']) && is_array($_POST['hobbies'])) ?
$_POST['hobbies'] : array();
if (isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
if (empty($username)) {
$errors[] = 'Username cannot be blank.';
}
// controllo se l'username è già stato utilizzato
$query = 'SELECT username FROM site_user WHERE username = "' .
$username . '"';
$result = mysql_query($query, $db) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$errors[] = 'Username ' . $username . ' is already registered.';
$username = '';
}
mysql_free_result($result);
if (empty($password)) {
$errors[] = 'Password cannot be blank.';
}
if (empty($first_name)) {
$errors[] = 'First name cannot be blank.';
}
if (empty($last_name)) {
$errors[] = 'Last name cannot be blank.';
}
if (empty($email)) {
$errors[] = 'Email address cannot be blank.';
}
if (count($errors) > 0) {
echo '
<strong style="color:#FF000;">Unable to process your ' .
'registration.[/b]</p>';
echo '
Please fix the following:</p>';
echo '<ul>';
foreach ($errors as $error) {
echo '[*]' . $error . '';
}
echo '[/list]';
} else {
// se non ci sono errori inserisco tutto nel DB
$query = 'INSERT INTO site_user
(user_id, username, password)
VALUES
(NULL, "' . mysql_real_escape_string($username, $db) . '", ' .
'PASSWORD("' . mysql_real_escape_string($password, $db) . '"))';
$result = mysql_query($query, $db) or die(mysql_error());
$user_id = mysql_insert_id($db);
$query = 'INSERT INTO site_user_info
(user_id, first_name, last_name, email, city, state, hobbies)
VALUES
(' . $user_id . ', ' .
'"' . mysql_real_escape_string($first_name, $db) . '", ' .
'"' . mysql_real_escape_string($last_name, $db) . '", ' .
'"' . mysql_real_escape_string($email, $db) . '", ' .
'"' . mysql_real_escape_string($city, $db) . '", ' .
'"' . mysql_real_escape_string($state, $db) . '", ' .
'"' . mysql_real_escape_string(join(', ', $hobbies), $db) . '")';
$result = mysql_query($query, $db) or die(mysql_error());
$_SESSION['logged'] = 1;
$_SESSION['username'] = $username;
header('Refresh: 5; URL=index.php');
?>
<html>
<head>
<title>Register</title>
</head>
<body>
[b]Thank you <?php echo $username; ?> for registering![/b]</p>
Your registration is complete! You are being sent to the page you
requested. If your browser doesn't redirect properly after 5 seconds,
[url="index.php"]click here[/url].</p>
</body>
</html>
<?php
die();
}
}
?>
<html>
<head>
<title>Register</title>
<style type="text/css">
td { vertical-align: top; }
</style>
</head>
<body>
<form action="register.php" method="post">
<table>
<tr>
<td><label for="username">Username:</label></td>
<td><input type="text" name="username" id="username" size="20"
maxlength="20" value="<?php echo $username; ?>"/></td>
</tr><tr>
<td><label for="password">Password:</label></td>
<td><input type="password" name="password" id="password" size="20"
maxlength="20" value="<?php echo $password; ?>"/></td>
</tr><tr>
<td><label for="email">Email:</label></td>
<td><input type="text" name="email" id="email" size="20" maxlength="50"
value="<?php echo $email; ?>"/></td>
</tr><tr>
<td><label for="first_name">First name:</label></td>
<td><input type="text" name="first_name" id="first_name" size="20"
maxlength="20" value="<?php echo $first_name; ?>"/></td>
</tr><tr>
<td><label for="last_name">Last name:</label></td>
<td><input type="text" name="last_name" id="last_name" size="20"
maxlength="20" value="<?php echo $last_name; ?>"/></td>
</tr><tr>
<td><label for="city">City:</label></td>
<td><input type="text" name="city" id="city" size="20" maxlength="20"
value="<?php echo $city; ?>"/></td>
</tr><tr>
<td><label for="state">State:</label></td>
<td><input type="text" name="state" id="state" size="2" maxlength="2"
value="<?php echo $state; ?>"/></td>
</tr><tr>
<td><label for="hobbies">Hobbies/Interests:</label></td>
<td><select name="hobbies[]" id="hobbies" multiple="multiple">
<?php
foreach ($hobbies_list as $hobby)
{
if (in_array($hobby, $hobbies)) {
echo '<option value="' . $hobby . '" selected="selected">' . $hobby .
'</option>';
} else {
echo '<option value="' . $hobby . '">' . $hobby . '</option>';
}
}
?>
</select></td>
</tr><tr>
<td> </td>
<td><input type="submit" name="submit" value="Register"/></td>
</tr>
</table>
</form>
</body>
</html>
File auth.inc.php
Codice PHP:
<?php
// comincia o continua la sessione
session_start();
if (!isset($_SESSION['logged'])) {
header('Refresh: 5; URL=login.php?redirect=' . $_SERVER['PHP_SELF']);
echo '
You will be redirected to the login page in 5 seconds.</p>';
echo '
If your browser doesn\'t redirect you properly automatically, ' .
'<a href="login.php?redirect=' . $_SERVER['PHP_SELF'] .
'">click here</a>.</p>';
die();
}
?>
File secret.php (potrai vederlo solo se hai eseguito correttamente il login)
Codice PHP:
<?php
include 'auth.inc.php';
?>
<html>
<head>
<title>Secret</title>
</head>
<body>
<h1>You've found my secret!</h1>
</body>
</html>
Come detto spero di averti messo tutto e se dovessero esserci dei problemi dimmi pure...