PROBLEMA Php e mysql

Pubblicità

MrAxsus

Nuovo Utente
Messaggi
144
Reazioni
7
Punteggio
38
Salve sto creando un area riservata con php, però mi da i seguenti errori:
Il codice è il sequente:
Codice:
<?php
include("dbconfig.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password received from loginform
$username=mysqli_real_escape_string($dbconfig,$_POST['username']);
$password=mysqli_real_escape_string($dbconfig,$_POST['password']);

$sql_query="SELECT id FROM user WHERE username='$username' and password='$password'";
$result=mysqli_query($dbconfig,$sql_query);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$count=mysqli_num_rows($result);


// If result matched $username and $password, table row must be 1 row
if($count==1)
{
$_SESSION['login_user']=$username;

header("location: areariservata.html");
}
else
{
$error="Username e Password non sono validi.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<meta charset="UTF-8">
<title>Area Riservata</title>
<style>
body {
    background-size: cover;
    font-family: Montserrat;
}

.logo {
    width: 213px;
    height: 36px;
    margin: 30px auto;
}

.login-block {
    width: 320px;
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    border-top: 5px solid #ff656c;
    margin: 0 auto;
}

.login-block h1 {
    text-align: center;
    color: #000;
    font-size: 18px;
    text-transform: uppercase;
    margin-top: 0;
    margin-bottom: 20px;
}

.login-block input {
    width: 100%;
    height: 42px;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #ccc;
    margin-bottom: 20px;
    font-size: 14px;
    font-family: Montserrat;
    padding: 0 20px 0 50px;
    outline: none;
}

.login-block input#username {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login-block input#username:focus {
    background: #fff url('http://i.imgur.com/u0XmBmv.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login-block input#password {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px top no-repeat;
    background-size: 16px 80px;
}

.login-block input#password:focus {
    background: #fff url('http://i.imgur.com/Qf83FTt.png') 20px bottom no-repeat;
    background-size: 16px 80px;
}

.login-block input:active, .login-block input:focus {
    border: 1px solid #ff656c;
}

.login-block button {
    width: 100%;
    height: 40px;
    background: #ff656c;
    box-sizing: border-box;
    border-radius: 5px;
    border: 1px solid #e15960;
    color: #fff;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 14px;
    font-family: Montserrat;
    outline: none;
    cursor: pointer;
}

.login-block button:hover {
    background: #ff7b81;
}

</style>
</head>

<body>

<div class="logo"></div>
<div class="login-block">
    <h1>Area Riservata</h1>
    <form method="post" action="" name="loginform">
    <input type="text" value="" placeholder="Username" id="username" name="username" />
    <input type="password" value="" placeholder="Password" id="password" name="password" />
    <button type="submit">Entra</button>
    </form>
</div>
</body>

</html>
 
Gli errori:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /storage/ssd1/120/2267120/public_html/login.php on line 12

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /storage/ssd1/120/2267120/public_html/login.php on line 13
 
Spero che il codice sia solo per studio e non implementato in un sito online.

Probabilmente la query ritorna un bel FALSE, per questo hai quegli errori.

Per vedere l'errore da cosa è provocato sostiruisci questa riga:
PHP:
$result=mysqli_query($dbconfig,$sql_query);

con:
PHP:
$result=mysqli_query($dbconfig,$sql_query) or die ("ERRORE: " . mysqli_error($dbconfig));
 
Oltre alle password in chiaro (cosa gravissima) non hai nel form token per prevenire i csrf attack.
 
Ti consiglierei di utilizzare soluzioni che ti mettano a disposizione già api e feature per sviluppare in modo strutturato, come Laravel o Symphony
 
Pubblicità
Pubblicità
Indietro
Top