I've just started developing using php.
I can't figure out why I keep getting a blank page with this code (which I was trying to get a login method with)
1 - I have a LAMP, apache seems to be correctly configured since with a simple test.php page i get the right page
2 - Die or an echo "something" won't display anything on my page inside the <?php ?> tag :C
I guess I'm missing some kind of lexical rule ... can you help me?
Thanks in advance
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it" xml:lang="it"/>
<head> <title>Ultimate Element - Domina il mondo tramite il potere degli elementi</title> </head>
<div> <img src="ultimate-element(1).png" alt="Ultimate Element" title="Utimate Element"> </img> </div>
<script language="javascript" type="text/javascript" src="js-test.js"> </script>
<input type="button" onclick="show_confirm()" value="La verita'" />
<input type="button" onclick="show_alert()" value="Clicca a destra" />
<input type="button" onclick="show_prompt()" value="Quanto sei stronzo?" />
<?php
$DB_host = "localhost";
$DB_user = "root";
$DB_password = "mysql";
$DB_name = "ultimate";
// connessione
$DB_connection = mysql_connect($DB_host, $DB_user, $DB_password) or die ('Errore durante la connessione');
$DB_selected = mysql_select_db($DB_name) or die ('Errore nel selezionare il database');
// se alla pagina sono state passate credenziali effettua il login con esse altrimenti mostra il form
if($_POST) { effettua_login(); }
else { mostra_form(); }
mostra_form()
{
?>
<form action="index.html" name="login_form" method="post">
<label>Nome:</label>
<input name="nome" type="text" value=""></input>
<label>Password:</label>
<input name="password" type="password" value=""></input>
<input name="invia" type="submit" value="Invia" />
</form>
<?
}
effettua_login()
{
// recupero il nome e la password inseriti dall'utente
$nome = trim($_POST['nome']);
$password = trim($_POST['password']);
// verifico se devo eliminare gli slash inseriti automaticamente da PHP
if(get_magic_quotes_gpc())
{
$nome = stripslashes($nome);
$password = stripslashes($password);
}
// verifico la presenza dei campi obbligatori
if(!$nome || !$password)
{
$messaggio = urlencode("Non hai inserito il nome o la password");
header("location: $_SERVER[PHP_SELF]?msg=$messaggio");
exit;
}
// effettuo l'escape dei caratteri speciali per inserirli all'interno della query
$nome = mysql_real_escape_string($nome);
$password = mysql_real_escape_string($password);
// preparo ed invio la query
$query = "SELECT id FROM utenti WHERE nome = '$nome' AND pswd = MD5('$password')";
$result = mysql_query($query) or die ("Errore nella query $query: ");
$record = mysql_fetch_array($result);
if(!$record)
{
$messaggio = urlencode('Nome utente o password errati');
header("location: $_SERVER[PHP_SELF]?msg=$messaggio");
exit;
}
else
{
session_start();
session_regenerate_id(TRUE);
$_SESSION['user_id'] = $record['id'];
$messaggio = urlencode('Login avvenuto con successo');
header("location: $_SERVER[PHP_SELF]?msg=$messaggio");
}
}
?>
</html>