Page 287 - PHP Novice to Ninja

Hello to everyone,
I created the login/logout system, but I have two questions.

. Once user logged in, how I can show her username?
. How I can add a profile page?

Thank you

That all depends on your code!

Post it and then we can answer…

I have these tables:

squadra: id | club | logo | allenatoreid
allenatore: id | username | password

this is my controller page:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php';

//***************************************************************************LOGIN
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/access.inc.php';

if (!userIsLoggedIn())
{
	include 'login.html.php';
	exit();
}


//***************************************************************************MERCATO

//MERCATO: Visualizzo la pagina del mercato
if (isset($_GET['mercato']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	
	//Recupero la lista di tutti i giocatori inseriti sul mercato
	try
	{
		$sql= "SELECT giocatore.id, nome, foto, nomeRuolo, valore, logo
		
				FROM giocatore
				
				INNER JOIN squadra
				ON squadraid= squadra.id
				
				INNER JOIN statoMercato
				ON statoMercatoid= statoMercato.id
				
				INNER JOIN ruolo
				ON ruoloid=ruolo.id
					
			WHERE stato= '/icone/mercato/si_mercato.png'";
			
		$result= $pdo->query($sql);
	}
	catch (PDOException $e)
	{
		$error= "Impossibile recuperare la lista dei giocatori inseriti sul mercato" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	foreach ($result as $row)
	{
		$giocatoriSulMercato[]= array('id'=>$row['id'] , 'foto'=>$row['foto'] , 'logo'=>$row['logo'] , 'nome'=>$row['nome'] , 'nomeRuolo'=>$row['nomeRuolo'] , 'valore'=>$row['valore']);
	}
	
	$pagetitle='Mercato';
	$action='compra';
	$button='Compra';
	
	include $_SERVER['DOCUMENT_ROOT'] . '/mercato/mercato.html.php';
	exit();
}

//MERCATO: Azione cliccando sul bottone COMPRA
if (isset($_GET['compra']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	
	try
	{
		$sql= 'INSERT INTO giocatore SET
			numero= :numero,
			nome= :nome,
			foto= :foto,
			ruoloid= :ruoloid,
			eta= :eta,
			giornoid= :giornoid,
			meseid= :meseid,
			annoid= :annoid,
			sofifa= :sofifa,
			tm= :tm,
			valore= :valore,
			statoMercatoid= :statoMercatoid,
			squadraid= :squadraid';
		
		$s= $pdo->prepare($sql);
		$s->bindValue(':numero' , $_POST['numero']);
		$s->bindValue(':nome' , $_POST['nome']);
		$s->bindValue(':foto' , $_POST['foto']);
		$s->bindValue(':ruoloid' , $_POST['ruolo']);
		$s->bindValue(':eta' , $_POST['eta']);
		$s->bindValue(':giornoid' , $_POST['giorno']);
		$s->bindValue(':meseid' , $_POST['mese']);
		$s->bindValue(':annoid' , $_POST['anno']);
		$s->bindValue(':sofifa' , $_POST['sofifa']);
		$s->bindValue(':tm' , $_POST['tm']);
		$s->bindValue(':valore' , $_POST['valore']);
		$s->bindValue(':statoMercatoid' , $_POST['stato']);
		$s->bindValue(':squadraid' , $_POST['squadra']);
		$s->execute();
	}
	catch(PDOException $e)
	{
		$error= "Impossibile aggiungere un nuovo giocatore". $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	header('Location: .');
	exit();
}



















//***************************************************************************PROFILO GIOCATORE

//GIOCATORE PROFILO: Visualizzo la pagina del profilo del giocatore
if (isset($_GET['codeg']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	
	
	//Recupero la lista dei giocatori appartenenti alla squadra selezionata
	try
	{
		$sql= "SELECT giocatore.id, numero, foto, nome, nomeRuolo, eta, giornoNascita, meseNascita, annoNascita, sofifa, tm, valore, 
		logo
			FROM giocatore
			INNER JOIN squadra
			ON squadraid= squadra.id
			
			INNER JOIN ruolo
			ON ruoloid= ruolo.id
			
			INNER JOIN giorno
			ON giornoid= giorno.id
			
			INNER JOIN mese
			ON meseid= mese.id
			
			INNER JOIN anno
			ON annoid= anno.id
			
			WHERE giocatore.id= :id";
			
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codeg']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile recuperare la lista dei giocatori appartenenti alla squadra selezionata" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();
	
	$pagetitle= 'Profilo giocatore';
	$numero= $row['numero'];
	$nome= $row['nome'];
	$foto= $row['foto'];
	$nomeRuolo= $row['nomeRuolo'];
	$eta= $row['eta'];
	$giornoNascita= $row['giornoNascita'];
	$meseNascita= $row['meseNascita'];
	$annoNascita= $row['annoNascita'];
	$sofifa= $row['sofifa'];
	$tm= $row['tm'];
	$logo=$row['logo'];
	$valore= $row['valore'];
	
	
	include $_SERVER['DOCUMENT_ROOT'] . '/giocatori/giocatore_profilo.html.php';
	exit();
}


























































//***************************************************************************PROFILO SQUADRA

//SQUADRA PROFILO: Visualizzo la pagina del profilo della squadra
if (isset($_GET['codes']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
	
	//Recupero la squadra selezionata
	try
	{
		$sql= 'SELECT club, logo FROM squadra WHERE id= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile recuperare la squadra selezionata" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();
	
	$pagetitle= 'Profilo societĂ ';
	$club= $row['club'];
	$logo= $row['logo'];
	
	//Recupero la lista dei giocatori appartenenti alla squadra selezionata
	try
	{
		$sql= "SELECT giocatore.id, numero, foto, nome, nomeRuolo, eta, giornoNascita, meseNascita, annoNascita, sofifa, tm, stato, valore
			FROM giocatore
			INNER JOIN squadra
			ON squadraid= squadra.id
			
			INNER JOIN ruolo
			ON ruoloid= ruolo.id
			
			INNER JOIN giorno
			ON giornoid= giorno.id
			
			INNER JOIN mese
			ON meseid= mese.id
			
			INNER JOIN anno
			ON annoid= anno.id
			
			INNER JOIN statoMercato
			ON statoMercatoid= statoMercato.id
			
			WHERE squadraid= :id
			
			ORDER BY FIELD (nomeRuolo, 'POR', 'LIB', 'ADA', 'TD', 'DC', 'TS', 'ASA', 'CDC' 'ED', 'CC', 'ES', 'COC', 'ATD', 'AT', 'ATS', 'AD', 'ATT', 'AS') ASC, nome";
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile recuperare la lista dei giocatori appartenenti alla squadra selezionata" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	foreach ($s as $row)
	{
		$giocatori[]= array('id'=>$row['id'] , 'numero'=>$row['numero'] , 'foto'=>$row['foto'] , 'nome'=>$row['nome'] , 'nomeRuolo'=>$row['nomeRuolo'] , 'eta'=>$row['eta'] , 'giornoNascita'=>$row['giornoNascita'] , 'meseNascita'=>$row['meseNascita'] , 'annoNascita'=>$row['annoNascita'] , 'sofifa'=>$row['sofifa'] , 'stato'=>$row['stato'] , 'tm'=>$row['tm'] , 'valore'=>$row['valore']);
	}
	
	//Recupero il numero di giocatori in rosa
	try
	{
		$sql= 'SELECT COUNT(*) AS numeroGiocatori FROM giocatore WHERE squadraid= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile prelevare il numero di giocatori in rosa" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();	
	$numeroGiocatori= $row['numeroGiocatori'];
	
	//Recupero il numero di giocatori in rosa
	try
	{
		$sql= 'SELECT CAST(AVG(eta) AS DECIMAL (10,1)) AS etĂ  FROM giocatore WHERE squadraid= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile prelevare il numero di giocatori in rosa" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();	
	$etĂ = $row['etĂ '];
	
	//Recupero nome stadio
	try
	{
		$sql= 'SELECT stadio FROM squadra WHERE squadra.id= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile prelevare il nome dello stadio" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();	
	$stadio= $row['stadio'];
	
	
	//Recupero valore squadra
	try
	{
		$sql= 'SELECT SUM(valore) AS valoreSquadra from giocatore WHERE squadraid= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile prelevare il valore della squadra" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();	
	$valoreSquadra= $row['valoreSquadra'];
	
	//Visualizzo il nome dell'allenatore
	try
	{
		$sql= 'SELECT squadra.id, nomeAllenatore, fotoAllenatore, psn, etĂ Allenatore FROM squadra
				INNER JOIN allenatore
					ON allenatoreid= allenatore.id
				WHERE squadra.id= :id';
		$s= $pdo->prepare($sql);
		$s->bindValue(':id' , $_GET['codes']);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= "Impossibile prelevare il nome dell'allenatore" . $e->getMessage();
		include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();	
	$nomeAllenatore= $row['nomeAllenatore'];
	$fotoAllenatore= $row['fotoAllenatore'];
	$etĂ Allenatore= $row['etĂ Allenatore'];
	$psn= $row['psn'];

	
	include $_SERVER['DOCUMENT_ROOT'] . '/squadre/squadra_profilo.html.php';
	exit();
}


//Visualizzo la lista delle squadre
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

try
{
	$result= $pdo->query('SELECT id, logo FROM squadra');
}
catch (PDOException $e)
{
	$error= 'Errore nel prelevare la lista delle squadre' . $e->getMessage();
	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc.html.php';
	exit();
}

foreach ($result as $row)
{
	$squadre[]= array('id'=>$row['id'], 'logo'=>$row['logo']);
}

include 'home.html.php';

this instead access.inc.php

<?php

function userIsLoggedIn()
{
	if (isset($_POST['action']) and $_POST['action'] == 'login')
	{
		if (!isset($_POST['username']) or $_POST['username'] == '' or !isset($POST['password']) or $_POST['password'] == '')
		{
			$GLOBALS['loginError'] = 'Riempire entrambi i campi';
			return FALSE;
		}
		
		$password= md5($_POST['password'] . 'mm');
		
		if (databaseContainsAllenatore($_POST['username'], $password))
		{
			session_start();
			$_SESSION['loggedIn'] = TRUE;
			$_SESSION['username'] = $_POST['username'];
			$_SESSION['password'] = $password;
			return TRUE;
		}
		else
		{
			session_start();
			unset($_SESSION['loggedIn']);
			unset($_SESSION['username']);
			unset($_SESSION['password']);
			$GLOBALS['loginError'] = 'Password o username errato';
			return FALSE;
		}
	}
	
	if (isset($_POST['action']) and $_POST['action'] == 'logout')
	{
		session_start();
			unset($_SESSION['loggedIn']);
			unset($_SESSION['username']);
			unset($_SESSION['password']);
			header('Location: ' . $_POST['goto']);
			exit;
	}
	
	session_start();
	if (isset($_SESSION['loggedIn']))
	{
		return databaseContainsAllenatore($_SESSION['username'],
			$_SESSION['password']);
	}
}

function databaseContainsAuthor($email, $password)
{
	include 'db.inc.php';
	
	try
	{
		$sql= 'SELECT COUNT(*) FROM allenatore
			WHERE username= :username AND password= :password';
		$s= $pdo->prepare($sql);
		$s->bindValue(':username' , $username);
		$s->bindValue(':password' , $password);
		$s->execute();
	}
	catch (PDOException $e)
	{
		$error= 'Errore in ricerca allenatore';
		include 'error.inc.html.php';
		exit();
	}
	
	$row= $s->fetch();
	
	if ($row[0] >0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

home.html.php

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Manual Manager</title>

<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="css/reset.css" rel="stylesheet" type="text/css">

</head>

<body>

<!--Contenitore sito-->
<div class="contenitore">
	
    
    
    
    <!--Intestazione-->
    <div class="intestazione">
		<div class="logo">
    		<img src="images/logo.png" alt="Manual Manager" width="150px"; height="120.5px">
		</div>
        
        <div class="search">
        	<form>
        		<input type="text" placeholder="Ricerca"	 class="search_input"/>
           	</form>
        </div>
        
        <div class="search_plus">
        	<form>
        		<input type="image" class="search_plus_input" src="images/search_plus.png" width="5px" height="5px" />
           	</form>
        </div>
    </div>
    
    
    
    
    <!--MenĂš-->
    <div class="menu">
    	<ul class="menuList">
        	<li><a href="#">Home</a></li>
            <li><a href="#">News</a></li>
            <li><a href="#">Calendario</a></li>
            <li><a href="?mercato">Mercato</a></li>
            <li><a href="#">Regolamento</a></li>
        </ul>
    </div>
    
    
    
    
    <!--News-->
    <div class="news">
    	<div class="titolo">
        	<h1>News</h1>
        </div>
    </div>
    



	<!--Squadre-->
    <div class="squadre">
    	<ul class="squadreList">
			<?php foreach ($squadre as $squadra): ?>
				<li>	
    				<form action="" method="post">
            			<input type="hidden" name="id" value="<?php echo($squadra['id']); ?>">
                		<a href="?codes=<?php echo($squadra['id']); ?>"> <img src="<?php echo ($squadra['logo']); ?>" width="50px" height="50px;" /> </a>   
                    </form>
   				</li>
 			<?php endforeach; ?>
		 </ul>
   	</div>





</div>

</body>
</html>

Clearly a copy and paste from somewhere :stuck_out_tongue:

But to answer your question, the username is stored in $_SESSION[‘username’]

Thank you.

In reference about book codes, I would integrate a registration page. Should I open another topic?

Yeah, I think you should open another topic if it digresses from the current topic.

1 Like

Where instead stored the username id?

Thank you

      Where instead stored the username id?

Someone help me please?
Thank you

I don’t think it is - you call this code:

if (databaseContainsAllenatore($_POST['username'], $password))
		{
			session_start();
			$_SESSION['loggedIn'] = TRUE;
			$_SESSION['username'] = $_POST['username'];
			$_SESSION['password'] = $password;
			return TRUE;
		}

which calls a function using the username and password to check whether the login is valid. But while the function might find the id, it does not return it - it seems to return “true” or “false” for whether they’re valid credentials. You could add another query inside the if() above to retrieve the id, or you could change the databaseContainsAllenatore() function so that it returns NULL for invalid credentials or the user-id for valid ones, then store it yourself.

Could me help me to add a query inside the if or to modify the function?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.