Codice PHP:
/*index.php*/
<?php session_start(); echo $_SESSION['username']; ?>
<!DOCTYPE html>
<html>
<head>
<title>index.php</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.14.1/math.min.js"></script>
<link href="config.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="form-riconoscimento">
<h4>modulo di riconoscimento</h4>
<button onclick="generaUsername()">genera username</button>
<table>
<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>
</table>
</div>
</body>
Codice PHP:
/*server.php*/
<?php
error_reporting(E_ALL);
session_start();
$host = "localhost";
$username = "root";
$password = "";
$db = "my_laboraccio";
$conex = mysqli_connect($host, $username, $password, $db);
/*variabili di index.php*/
$uname = $_POST['username'];
$pw = $_POST['password'];
$opzioneUtente = $_POST['opzioneUtente'];
function registraUtente(){
global $uname;
echo $uname;
global $pw;
global $conex;
$sql = "SELECT * FROM utenti WHERE username='$uname'";
$resource = mysqli_query($conex, $sql);
$result = mysqli_num_rows($resource);
echo $result;
if($result == 0) {
$sql = "INSERT INTO utenti (id, username, password) VALUES (NULL, '$uname', '$pw')";
mysql_query($conex, $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;
global $conex;
$sql = "SELECT * FROM utenti WHERE username='$uname' AND password='$pw'";
$resource = mysqli_query($conex, $sql);
$result = mysqli_num_rows($resource);
if($result == 1) {
$_SESSION['username'] = $uname;
$_SESSION['password'] = $pw;
header("Refresh:10 ;url=http://www.laboraccio.altervista.org/debug/mysqli/profilo.php");
}
elseif($result == 0) {print "utente non valido";
header("Refresh:10; url=http://www.laboraccio.altervista.org/debug/mysqli/index.php");
}
}
if($opzioneUtente == 'registrareUtente'){
registraUtente();
}
elseif($opzioneUtente == "loggareUtente") {
loggareUtente();
}
?>
Codice PHP:
<?php
error_reporting(E_ALL);
session_start();
echo $_SESSION['username'];
/*
if(!isset($_SESSION['username'])){
header("Refresh:10 ;url=http://laboraccio.altervista.org/debug/mysqli/index.php");
}*/
?>
dopo aver fatto il login, passo per la pagina del server e poi una volta arrivato alla pagina del profilo mi da questo errore
Notice: Undefined index: username in /membri/laboraccio/debug/mysqli/profilo.php on line 5
poi se torno al index, mi fa vedere normalmente il mio nome utente.