voglio usare mysqli visto che siete in tanti a consigliarmelo, purtroppo mi da questo errore
Warning: mysqli_connect(): (HY000/1044): Access denied for user 'laboraccio'@'localhost' to database 'mylaboraccio' in /membri/laboraccio/debug/mysqli/server.php on line 4
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /membri/laboraccio/debug/mysqli/server.php on line 5
Notice: Undefined index: sms in /membri/laboraccio/debug/mysqli/server.php on line 13
Notice: Undefined index: sessione in /membri/laboraccio/debug/mysqli/server.php on line 15
Notice: Undefined variable: conex in /membri/laboraccio/debug/mysqli/server.php on line 47
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /membri/laboraccio/debug/mysqli/server.php on line 47
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /membri/laboraccio/debug/mysqli/server.php on line 47
Codice PHP:
/*index.php*/
<form method="post" action="server.php">
<tr>
<td><input type="text" name="username" id="username" required></td>
</tr>
<tr>
<td><input type="password" name="password" placeholder="password"></td>
</tr>
<tr>
<td><input type="radio" name="opzioneUtente" value="registrareUtente">registrasi</td></tr>
<tr>
<td><input type="radio" name="opzioneUtente" value="loggareUtente" checked>accedere</td>
</tr>
<tr>
<td><input type="submit" value="invio"></td></tr>
</form>
Codice PHP:
<?php
/*server.php*/
error_reporting(E_ALL);
session_start();
$conex = mysqli_connect('localhost', 'root', '', "mylaboraccio");
mysqli_select_db("my_laboraccio", $conex);
/*variabili di index.php*/
$uname = $_POST['username'];
$pw = $_POST['password'];
$opzioneUtente = $_POST['opzioneUtente'];
/*variabili di profilo.php */
echo $_GET['sms'];
if($_POST['sessione'] == "logout") {
session_destroy();
/*header("Refresh:10; url=http://www.laboraccio.altervista.org/debug/index.php");*/
echo "sessione scaduta";
}
function registraUtente(){
global $uname;
echo $uname;
global $pw;
$sql = "SELECT * FROM utenti WHERE username='$uname'";
$resource = mysqli_query($conex, $sql) or die (mysql_error());
$result = mysqli_num_rows($conex, $resource);
echo $result;
if($result == 0) {
$sql = "INSERT INTO utenti (id, username, password) VALUES (NULL, '$uname', '$pw')";
mysql_query($sql);
header("Refresh:10; url=http://www.laboraccio.altervista.org/debug/index.php");
echo "nuovo utente creato";
echo "registrazione completata";
}
elseif($result == 1){
header("Refresh:10; url=http://www.laboraccio.altervista.org/debug/index.php");
echo "username gia in uso, riprova con un altro";
}
}
function loggareUtente(){
global $uname;
global $pw;
$sql = "SELECT * FROM utenti WHERE username='$uname' AND password='$pw'";
$resource = mysqli_query($conex, $sql) or die (mysqli_error());
$result = mysqli_num_rows($conex, $resource);
if($result == 1) {
$_SESSION['username'] = $uname;
$_SESSION['password'] = $pw;
header("Location:http://www.laboraccio.altervista.org/debug/profilo.php");
}
elseif($result == 0) {print "utente non valido";
header("Refresh:10; url=http://www.laboraccio.altervista.org/debug/index.php");
}
}
if($opzioneUtente == 'registrareUtente'){
registraUtente();
}
elseif($opzioneUtente == "loggareUtente") {
loggareUtente();
}
?>
Codice PHP:
*/profilo.php*/
<?php
error_reporting(E_ALL);
session_start();
if(isset($_SESSION['username'])){
echo $_SESSION['username'];
}
else if(!isset($_SESSION['username'])){
header("location: http://laboraccio.altervista.org/debug/index.php");
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<style>
div#chat {
width:500px;
height:500px;
border: solid black 2px;
}
div#messaggio input[type="text"] {
width:100%;
padding:10px;
}
</style>
</head>
<script type="text/javascript">
function sendSms(){
sms = document.getElementById("sms").value;
chat = document.getElementById("chat");
console.log(sms);
ajax = new XMLHttpRequest();
ajax.open("get", "server.php?sms="+sms, true);
ajax.send();
ajax.onreadystatechange = function(){
if(ajax.readyState == 4 && ajax.status == 200){
console.log("ajax: "+ajax.responseText);
chat.innerHTML = ajax.responseText;
}
}
}
</script>
<body>
<div id="chat"></div>
<div id="messaggio">
<input type="text" name="sms" id="sms" placeholder="inserisci il tuo messaggio qui">
<button onclick="sendSms()" id="submit">invio</button>
</div>
<div>
<form method="post" action="server.php">
<input type="radio" name="sessione" value="logout">logout
<input type="submit" value="invio">
</form>
</div>
<body>