Ciao a tutti. Sono nuovo del forum e...nuovo di php!! Vi chiedo una mano nei miei studi di php e scusate il thread lungo
Sto testando una procedura di autenticazione (esattamente come riportato nel libro) ma la cosa non funzia. Sono ore che ci sbatto la testa...:crying:
La procedura in teoria:
Apro il file "secret.php" , il quale include auth.inc.php
----secret.php
<?php
include 'auth.inc.php';
?>
<html>
<head>
<title>Secret</title>
</head>
<body>
<h1>You've found my password!</h1>
</body>
</html>
-----------------
-----auth.inc.php----
<?php
// start or continue session
session_start();
if (!isset($_SESSION['logged']) || $_SESSION['logged'] != 1) {
header('Refresh: 5; URL=login.php?redirect=' . $_SERVER['PHP_SELF']);
echo '<p>You will be redirected to the login page in 5 seconds.</p>';
echo '<p>If your browser doesn\’t redirect you properly automatically, ' .
'<a href="login.php?redirect=' . $_SERVER['PHP_SELF'] .' ">click here</a>.</p>';
die();
}
?>
-------------------------------
Non essendomi ancora loggato DOVREI essere reindirizzato al file login.php MA QUESTO NON ACCADE.
Apro allora il file login.php. Digito le password(al momento indicate nel file stesso). DOVREI essere indirizzato al file secret.php, ma NON SOLO questo non avviene, username e passwors( asterischi) rimangono lì statici come se non avvenissene nessuna azione!!
Eppure nel libro lo da per funzionante! :gun:
Sicuramente c'è un errore, ma non riesco a trovarlo...mi date una mano?
---------login.php-------
<?php
session_start();
include 'db.inc.php';
// filter incoming values
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$redirect = (isset($_REQUEST['redirect'])) ? $_REQUEST['redirect'] : 'secret.php';
if (isset($_POST['submit'])) {
if (!isset($_SESSION['logged']) || $_SESSION['logged']!= 1) {
if (!empty($_POST['username']) && $_POST['username'] == 'wroxbooks' && !empty($_POST['password']) && $_POST['password'] == 'aregreat') {
$_SESSION['username'] = $username;
$_SESSION['logged'] = 1;
header ('Refresh: 5; URL=' . $redirect);
echo "<p>You will be redirected to your original page request.</p>";
echo '<p>If your browser doesn't redirect you properly automatically, ' .
'<a href="' . $redirect . '">click here</a>.</p>';
die();
} else {
// set these explicitly just to make sure
$_SESSION['username'] = '';
$_SESSION['logged'] = 0;
$error = '<p><strong>You have supplied an invalid username and/or ' .
'password!</strong>Please <a href="register.php">click here ' .
'to register</a>if you have not done so already.</p>';
}
}
}
?>
<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>
------------------------------------------------
Grazie mille a chi mi aiuterà a capire dov'è l'errore o la mancanza di qualcosa.
PS. Ho postato la stessa richiesta sul forum php di Html.it...ma forse li non amano i newbye..
Buona giornata a tutti
Sto testando una procedura di autenticazione (esattamente come riportato nel libro) ma la cosa non funzia. Sono ore che ci sbatto la testa...:crying:
La procedura in teoria:
Apro il file "secret.php" , il quale include auth.inc.php
----secret.php
<?php
include 'auth.inc.php';
?>
<html>
<head>
<title>Secret</title>
</head>
<body>
<h1>You've found my password!</h1>
</body>
</html>
-----------------
-----auth.inc.php----
<?php
// start or continue session
session_start();
if (!isset($_SESSION['logged']) || $_SESSION['logged'] != 1) {
header('Refresh: 5; URL=login.php?redirect=' . $_SERVER['PHP_SELF']);
echo '<p>You will be redirected to the login page in 5 seconds.</p>';
echo '<p>If your browser doesn\’t redirect you properly automatically, ' .
'<a href="login.php?redirect=' . $_SERVER['PHP_SELF'] .' ">click here</a>.</p>';
die();
}
?>
-------------------------------
Non essendomi ancora loggato DOVREI essere reindirizzato al file login.php MA QUESTO NON ACCADE.
Apro allora il file login.php. Digito le password(al momento indicate nel file stesso). DOVREI essere indirizzato al file secret.php, ma NON SOLO questo non avviene, username e passwors( asterischi) rimangono lì statici come se non avvenissene nessuna azione!!
Eppure nel libro lo da per funzionante! :gun:
Sicuramente c'è un errore, ma non riesco a trovarlo...mi date una mano?
---------login.php-------
<?php
session_start();
include 'db.inc.php';
// filter incoming values
$username = (isset($_POST['username'])) ? trim($_POST['username']) : '';
$password = (isset($_POST['password'])) ? $_POST['password'] : '';
$redirect = (isset($_REQUEST['redirect'])) ? $_REQUEST['redirect'] : 'secret.php';
if (isset($_POST['submit'])) {
if (!isset($_SESSION['logged']) || $_SESSION['logged']!= 1) {
if (!empty($_POST['username']) && $_POST['username'] == 'wroxbooks' && !empty($_POST['password']) && $_POST['password'] == 'aregreat') {
$_SESSION['username'] = $username;
$_SESSION['logged'] = 1;
header ('Refresh: 5; URL=' . $redirect);
echo "<p>You will be redirected to your original page request.</p>";
echo '<p>If your browser doesn't redirect you properly automatically, ' .
'<a href="' . $redirect . '">click here</a>.</p>';
die();
} else {
// set these explicitly just to make sure
$_SESSION['username'] = '';
$_SESSION['logged'] = 0;
$error = '<p><strong>You have supplied an invalid username and/or ' .
'password!</strong>Please <a href="register.php">click here ' .
'to register</a>if you have not done so already.</p>';
}
}
}
?>
<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>
------------------------------------------------
Grazie mille a chi mi aiuterà a capire dov'è l'errore o la mancanza di qualcosa.
PS. Ho postato la stessa richiesta sul forum php di Html.it...ma forse li non amano i newbye..
Buona giornata a tutti