Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13

Discussione: Problema script

  1. #1

    Problema script

    Intanto ciao a tutti. Vi espongo il problema.
    Ho uno script per il login in php. Ora vi posto i codici se puo essere utile a qualcuno:

    connessione.php
    codice:
    <?php
    
    $db_host = "localhost"; 
    $db_user = " ";
    $db_pass = " ";
    $db_name = " ";
    
    $db_conn = mysql_connect($db_host, $db_user, $db_pass) or die ('Errore connessione :' . mysql_error());
    mysql_select_db($db_name, $db_conn);
    
    ?>
    controllo.php
    codice:
    <?
    
    include ('connessione.php');
    
    session_start();
    $logkey = $_SESSION['logkey'];
    
    if (!isset($logkey)) {
    	echo 'Errore non sei autenticato! Effettua il login';
    	exit();
    }
    
    $query = "SELECT * FROM utenti WHERE logkey = '$logkey'";
    $result = mysql_query($query, $db_conn) or die ('Errore query : ' . mysql_error());
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows != 1) {
    	echo 'Errore non sei autenticato! Effttua il login';
    	exit();
    } else {
    	$row = mysql_fetch_assoc($result);
    	}
    
    ?>
    Function_random.php
    codice:
    <?php
    
    function SessioneCasuale(){
    	$N_Caratteri = 32;
    	$Stringa = "";
    	For($I=0;$I<$N_Caratteri;$I++){
    		do{
    			$N = Ceil(rand(48,122));
    		}while(!((($N >= 48) && ($N <= 57)) || (($N >= 65) && ($N <= 90)) || (($N >= 97) && ($N <= 122))));
    			
    		
    		$Stringa = $Stringa.Chr ($N);
    	}
    	return $Stringa;
    }
    
    ?>
    login.html
    codice:
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Documento senza titolo</title>
    </head>
    
    <body>
    <form id="form1" name="form1" method="post" action="script.php">
    <table width="200" border="1" align="center">
      <tr>
        <td>Username</td>
        <td><label>
          <input type="text" name="username" id="username" />
        </label></td>
      </tr>
      <tr>
        <td>Password</td>
        <td><input type="text" name="password" id="password" /></td>
      </tr>
      <tr>
        <td></td>
        <td>
          <label>
            <input type="submit" name="button" id="button" value="Invia" />
            </label>
        </td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    privata.php
    codice:
    <?
    include ('controllo.php');
    ?>
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Documento senza titolo</title>
    </head>
    
    <body>
    Benvenuto caro, <? echo $row['username']; ?>
    </body>
    </html>
    registrazione.html
    [code[<!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Documento senza titolo</title>
    </head>

    <body>
    <form id="form1" name="form1" method="post" action="script_iscriviti.php">
    <table width="200" border="1" align="center">
    <tr>
    <td>Name</td>
    <td><label>
    <input type="text" name="name" id="name" />
    </label></td>
    </tr>
    <tr>
    <td>Surname</td>
    <td><input type="text" name="surname" id="surname" /></td>
    </tr>
    <tr>
    <td>Username</td>
    <td><input type="text" name="username" id="username" /></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="text" name="password" id="password" /></td>
    </tr>
    <tr>
    <td></td>
    <td>
    <label>
    <input type="submit" name="button" id="button" value="Invia" />
    </label>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    [/code]

    script.php (quello che effettua il login)
    codice:
    <?php
    
    include ('connessione.php');
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $query = "SELECT * FROM utenti WHERE username = '$username' AND password = '$password'";
    
    $result = mysql_query($query, $db_conn);
    $num_rows = mysql_num_rows($result);
    
    if ($num_rows != 1) {	
    	echo 'Errore Useraname o Password non presenti sul database Effettua il login';
    
    } else {
    	session_start();
    	
    	include_once ('Function_random.php');
    	
    	$logkey = SessioneCasuale();
    	
    	$insert = "UPDATE utenti SET logkey = '$logkey' WHERE username = '$username'";
    	$res_insert = mysql_query($insert) or die('Errore query: ' . mysql_error());
    	
    	$_SESSION['logkey'] = $logkey;
    	
    	header ("Location: privata.php");
    	}
    
    ?>
    script_iscriviti.php (colui che elabora i dati di registrazione)
    codice:
    <?php
    
    // Includo i file per la connessione al DB
    include ('connessione.php');
    
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    // Controllo sul database se è già presente un username uguale
    $db_query = "SELECT * FROM utenti WHERE username = '$username'";
    $rs = mysql_query($db_query, $db_conn) or die ('Errore query: ' . mysql_error());
    $num_rows = mysql_num_rows($rs);
    
    if ($num_rows != 0) {
    
    	echo 'Errore Username gi&agrave; presente sul database! Torna Indietro!'
    	exit();
    
    }
    
    // Inserisco i dati nel DB
    $query =  "INSERT INTO `utenti` (`id`, `name`, `surname`, `username`, `password`) 
    						VALUES ('id', '$name', '$surname', '$username', '$password')";
    
    $result = mysql_query($query, $db_conn) or die ('Errore query: ' . mysql_error());
    
    mysql_close($db_conn);
    
    header ("Location: login.php");
    
    ?>
    tabella.sql
    codice:
    CREATE TABLE utenti (
       id INT UNSIGNED NOT NULL AUTO_INCREMENT,
       name VARCHAR(30) NOT NULL,
       surname VARCHAR(30) NOT NULL,
       username VARCHAR(30) NOT NULL,
       password CHAR(32) NOT NULL,
       PRIMARY KEY(id),
       INDEX(username, password)
    );
    Allora, hostato i files. Ho cominciato creando la tabella nal database ed inserendo i dati nel file connessione.php. Ho aperto registrazione.html ed ho compilato il form, dopodiche ho clikkato su invia.
    E qui sta il problema, compare questo errore:
    codice:
    PHP Parse error: syntax error, unexpected T_EXIT, expecting ',' or ';' in C:\WebSites\pointpc.org_xfpxtrniyph8omi2\login\sistema_login\script_iscriviti.php on line 19
    . La riga che indica l'errore (on line 19) è la seguente:
    codice:
    echo 'Errore Username gi&agrave; presente sul database! Torna Indietro!'
    	exit();

    Sapete dirmi come risolvere? Grazie mille!

  2. #2
    manca il ; alla fine della riga
    provalo così:
    Codice PHP:
    echo 'Errore Username già presente sul database! [url="registrazione.html"]Torna Indietro![/url]';

        exit(); 
    Never care for what they say, Never care for games they play. Never care for what they do, Never care for what they know.
    «Solo due cose sono infinite: l’universo e la stupidità umana, e sul primo non sono sicuro». (A. Einstein)

  3. #3
    ora provo. Ti so dire

  4. #4
    Ora compare questo errore:
    codice:
    Errore query: Incorrect integer value: 'id' for column 'id' at row 1
    Qualche soluzione?

  5. #5
    ne sapete qualcosa?

  6. #6
    nella insert c'è 'id' e quindi gli passi una stringa è corretto?

    io mi aspettavo di trovar qualcosa tipo $id per di più guaradndo sopra non c'è un

    $id = $_POST['id'];

    ok ammetto di non averlo provato

    ciao

  7. #7
    In fin dei conti cosa dovrei fare?

  8. #8
    cambia così
    Codice PHP:
    <?php

    // Includo i file per la connessione al DB
    include ('connessione.php');

    $name $_POST['name'];
    $surname $_POST['surname'];
    $username $_POST['username'];
    $password $_POST['password'];

    // Controllo sul database se è già presente un username uguale
    $db_query "SELECT * FROM utenti WHERE username = '$username'";
    $rs mysql_query($db_query$db_conn) or die ('Errore query: ' mysql_error());
    $num_rows mysql_num_rows($rs);

    if (
    $num_rows != 0) {

        echo 
    'Errore Username già presente sul database! [url="registrazione.html"]Torna Indietro![/url]'
        
    exit();

    }

    // Inserisco i dati nel DB
    $query =  "INSERT INTO `utenti` (`name`, `surname`, `username`, `password`) 
                            VALUES ('
    $name', '$surname', '$username', '$password')";

    $result mysql_query($query$db_conn) or die ('Errore query: ' mysql_error());

    mysql_close($db_conn);

    header ("Location: login.php");

    ?>
    Sei alle prime armi con PHP vero ?
    Never care for what they say, Never care for games they play. Never care for what they do, Never care for what they know.
    «Solo due cose sono infinite: l’universo e la stupidità umana, e sul primo non sono sicuro». (A. Einstein)

  9. #9
    In effetti Ora provo subito...

  10. #10
    Perfetto, ora cis ono problemi con il databse. La tabella. Dovrei aggiungere il campo logkey ma non so come impostarlo.
    La tabella per ora è cosi:
    [code ]CREATE TABLE utenti (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(30) NOT NULL,
    surname VARCHAR(30) NOT NULL,
    username VARCHAR(30) NOT NULL,
    password CHAR(32) NOT NULL,
    PRIMARY KEY(id),
    INDEX(username, password)
    );[/code]
    Come faccio?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.