Je m'excuse d'avance si je pose une question qui pourrait être facile pour certains d'entre vous. J'ai quelques erreurs avec mon site Web. Il fonctionne sur mon environnement de développement local, mais sur la plate-forme d'hébergement que j'utilise (000webhost), je reçois les erreurs suivantes sur les pages suivantes:site fonctionne sur le développement local, mais les erreurs sur la plate-forme d'hébergement
Page d'enregistrement (pt-5-register.php) - Quand je remplis tous les détails de la page, j'obtiens alors l'erreur suivante: Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd4/573/2779573/public_html/pt-5-register.php:1) in /storage/ssd4/573/2779573/public_html/pt-5-register.php on line 123
. Sur le fichier "pt-5-register.php", j'ai noté la ligne où l'erreur s'est produite. Attention à la ligne 123. Image of attempted registration - username: 1st.test
Cependant, lorsque je me connecte à la base de données, un utilisateur nouvellement créé est visible. J'ai même créé un script pour un journal, qui enregistre l'heure et la date. L'heure et la date correspondent à l'heure à laquelle j'ai tenté de m'inscrire. Malheureusement, le nom des utilisateurs n'apparaît pas dans la base de données.
je vais ensuite sur la page de connexion pour savoir ce qui se passe, entrez les détails que je « enregistré » avec, et obtenir les erreurs suivantes:
Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 47
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 105
Notice: Undefined variable: link in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /storage/ssd4/573/2779573/public_html/pt-5-login.php on line 110
Sur le « pt-5-login. php ", j'ai noté la ligne où l'erreur s'est produite. Attention pour la ligne 47, 105 et 110.
Les codes suivants sont les pages où les erreurs sur ce site est en cours. Un lien vers le site sera envoyé sur demande.
S'il vous plaît aider, car je crois que je ne suis pas loin de compléter ce système de connexion/enregistrement.
Voici le code PHP POUR LA CONFIGURATION PAGE "config.php"
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define("DB_SERVER", "localhost");
define("DB_USERNAME", "***********");
define("DB_PASSWORD", "***********");
define("DB_DATABASE", "***********");
// Attempt to connect to MySQL database
$connect = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
// Check connection
if($connect === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
LE CODE SUIVANT EST PHP/HTML de la page REGISTER "pt-5 register.php"
<!-- ----------------------------------------------------------------------- */
/* Taken from "Tutorial Republic - PHP MySQL Login System" -
/* ------------------------------------------------------------------------ -->
----PHP CODE----
<?php
// Include config file
require_once 'config.php';
// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// Validate username
if(empty(trim($_POST["username"])))
{
$username_err = "Please enter a username.";
}
else
{
// Prepare a select statement
$sql = "SELECT id FROM users WHERE username = ?";
if($stmt = mysqli_prepare($connect, $sql))
{
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = trim($_POST["username"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt))
{
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1)
{
$username_err = "This username is already taken.";
}
else
{
$username = trim($_POST["username"]);
}
}
else
{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Validate password
if(empty(trim($_POST['password'])))
{
$password_err = "Please enter a password.";
}
elseif(strlen(trim($_POST['password'])) < 6)
{
$password_err = "Password must have atleast 6 characters.";
}
else
{
$password = trim($_POST['password']);
}
// Validate confirm password
if(empty(trim($_POST["confirm_password"])))
{
$confirm_password_err = 'Please confirm password.';
}
else
{
$confirm_password = trim($_POST['confirm_password']);
if($password != $confirm_password)
{
$confirm_password_err = 'Password did not match.';
}
}
// Check input errors before inserting in database
if(empty($username_err) && empty($password_err) && empty($confirm_password_err))
{
// Prepare an insert statement
$sql = "INSERT INTO users (username, password) VALUES (?, ?)";
if($stmt = mysqli_prepare($connect, $sql))
{
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
// Set parameters
$param_username = $username;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt))
{
// Redirect to login page
header("location: pt-5-login.php"); // LINE 123 - ERROR OCCURRED
}
else
{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($connect);
}
?>
<!-- ----------------------------------------------------------------------------------- */
/* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
/* ------------------------------------------------------------------------------------ -->
----HTML CODE----
<!doctype html>
<html>
<head>
<title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
<meta name="description" content="Learning Portal">
<meta name="keywords" content="Learning Portal EFREI">
<meta name="author" content="D.Jackson">
<link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
<link rel="icon" type="image/ico" href="favicon.ico">
<link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
</head>
<!-- <div> is to group block elements for CSS formatting -->
<body>
<div id="container">
<header><a href="pt-5-home.htm">Register</a>
</header>
<!-- The htm. above which would connect to the main page was originally
"mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
<!--
<div id="main">
<section>
<p>Please enter your details:
</p>
-->
<!-- ----------------------------------------------------------------------- */
/* Resuming "Tutorial Republic - PHP MySQL Login System"
https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
/* ------------------------------------------------------------------------ -->
<!--
</head>
<body>
-->
<div class="wrapper">
<h2>Sign Up</h2>
<p>Please fill this form to create an account.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group
<?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username:<sup>*</sup></label>
<input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group
<?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password:<sup>*</sup></label>
<input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group
<?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
<label>Confirm Password:<sup>*</sup></label>
<input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
<span class="help-block"><?php echo $confirm_password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
<p>Already have an account? <a href="pt-5-login.php">Login here</a>.</p>
</form>
</div>
</body>
</html>
Voici le PHP/HTML CODE dE lA PAGE LOGIN "pt-5-login.php"
<!-- ----------------------------------------------------------------------- */
/* Taken from "Tutorial Republic - PHP MySQL Login System"
/* ------------------------------------------------------------------------ -->
----PHP CODE----
<?php
// Include config file
require_once 'config.php';
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// Check if username is empty
if(empty(trim($_POST["username"])))
{
$username_err = 'Please enter username.';
}
else
{
$username = trim($_POST["username"]);
}
// Check if password is empty
if(empty(trim($_POST['password'])))
{
$password_err = 'Please enter your password.';
}
else
{
$password = trim($_POST['password']);
}
// Validate credentials
if(empty($username_err) && empty($password_err))
{
// Prepare a select statement
$sql = "SELECT username, password FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)) // LINE 47 - ERROR OCCURRED
}
{
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = $username;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt))
{
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1)
{
// Bind result variables
mysqli_stmt_bind_result($stmt, $username, $hashed_password);
if(mysqli_stmt_fetch($stmt))
{
if(password_verify($password, $hashed_password))
{
/* Password is correct, so start a new session and
save the username to the session */
session_start();
$_SESSION['username'] = $username;
header("location: welcome.php");
}
else
{
// Display an error message if password is not valid
$password_err = 'The password you entered was not valid.';
}
}
}
else
{
// Display an error message if username doesn't exist
$username_err = 'No account found with that username.';
}
}
else
{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt); // LINE 105 - ERROR OCCOURED
}
}
// Close connection
mysqli_close($link); // LINE 110 - ERROR OCCOURED
}
}
?>
<!-- ----------------------------------------------------------------------------------- */
/* Temporary suspension of "Tutorial Republic - PHP MySQL Login System" Tutorial
/* ------------------------------------------------------------------------------------ -->
----HTML CODE----
<!doctype html>
<html>
<head>
<title>Learning Portal</title> <!-- CHANGE THESE IN ALL FILES RELATING TO THIS PROJECT! -->
<meta name="description" content="Learning Portal">
<meta name="keywords" content="Learning Portal EFREI">
<meta name="author" content="D.Jackson">
<link rel="stylesheet" type="text/css" href="NHS.css" media="screen">
<link rel="icon" type="image/ico" href="favicon.ico">
<link href="https://fonts(dot)googleapis(dot)com/css?family=Jura" rel="stylesheet">
</head>
<!-- <div> is to group block elements for CSS formatting -->
<body>
<div id="container">
<header><a href="pt-5-home.htm">Login</a>
</header>
<!-- The htm. above which would connect to the main page was originally
"mars.htm", then "NHS.htm" and is now called "pt-5-home.htm" -->
<!--
<div id="main">
<section>
<p>Please enter your details:
</p>
-->
<!-- ----------------------------------------------------------------------- */
/* Resuming "Tutorial Republic - PHP MySQL Login System"
https://www.tutorialrepublic.com/php-tutorial/php-mysql-login-system.php
/* ------------------------------------------------------------------------ -->
<body>
<div class="wrapper">
<h2>Login</h2>
<p>Please fill in your credentials to login.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Username:<sup>*</sup></label>
<input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
<label>Password:<sup>*</sup></label>
<input type="password" name="password" class="form-control">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
</div>
<p>Don't have an account? <a href="pt-5-register.php">Sign up now</a>.</p>
</form>
</div>
</body>
</html>
souvent besoin de modifier votre mot de passe. – Erik
'$ link' devrait être' $ connect', n'est-ce pas? (Je suppose que c'est défini dans config.php?) – Jeff
et je suppose que le commentaire 'pris du tutoriel ... --- Code PHP -' est _not_ dans votre code réel, non? – Jeff