Strumenti Utente

Strumenti Sito


software:php

$_SESSION

classi php

qui informazioni: https://www.html.it/pag/18341/creare-le-classi/

dichiarazione della classe

class MyClass {
     // implementazione della classe MyClass...
}
class MyClass {
    // variabili membro
    public $a = 10;
    public $b = 20;
    // funzioni
    public function sayHello() {
            echo "Hello!";
    }
}

uso della classe

$myClass_1 = new MyClass();
// stampa "Hello!"
$myClass_1->sayHello();
// stampa 10
echo $myClass_1->a;

autenticazione

INFO

PHP

il casino è che dalla debian 9 è cambiata la versione di default del php che dalla 5 è passata alla 7.
Naturalmente su apache il php è caricato di default e per vedere le impostazioni del php.ini possiamo creare un file da mettere in /var/www/html ad esempio info.php

<?php
phpinfo();
?>

info.php
  <?php
  phpinfo();
  ?>

e vedere così com'è la situazione

ho capito che se si vuole creare un database nuovo per costruire qualcosa e inserire i dati ad esempio una pagina web, è bene dare ad un utente nuovo specifico per quel database i diritti per fare gli inserimenti e usare poi quell'utente da php.
quindi creiamo il database da riga di comando mariadb

create database prova;

poi creare tabelle

e qui dovro mettere il codice\\

poi creare l'utente specifico:

grant all privileges on database.* to 'nomeutentespecifico'@'localhost' identified by 'passwordutente';

altro file:

ins.php
<html>
<head>
</head>
<body>
 
<?php
 
$dbname = 'tato';
$dbuser = 'tato';
$dbpass = 'tatone';
$dbhost = 'localhost';
 
$link = new mysqli($dbhost, $dbuser, $dbpass, $dbname) ;
// Check connection
            if ($link->connect_error) {
               die("Connection failed: " . $link->connect_error);
            }
 
//mysqli_select_db($dbname)
//or die("Could not open the db '$dbname'");
 
 
 
//$name = $_POST['name'];
//$address = $_POST['address'];
 
 
// $toinsert = "INSERT INTO anagrafica (name, address) VALUES ('$name','$address')";
$sql = "INSERT INTO anagrafica (name, address) VALUES ('".$_POST["name"]."','".$_POST["address"]."')";
 
 
 
 
if (mysqli_query($link, $sql)) {
               echo "New record created successfully";
            } else {
               echo "Error: " . $sql . "" . mysqli_error($link);
            }
            $link->close();
 
 
 
?>
test
</body>
</html>

form html

form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 
<head>
<title>PAGINA CARICAMENTO DATI</title>
</head>
 
<body>
<table border="0">
  <tr>
    <td align="center">Inserisci i dati richiesti</td>
  </tr>
  <tr>
    <td>
      <table>
        <form method="post" action="creadiretto.php">
        <tr>
          <td>Nome</td>
          <td><input type="text" name="name" size="20">
          </td>
        </tr>
        <tr>
          <td>Indirizzo</td>
          <td><input type="text" name="address" size="40">
          </td>
        </tr>
        <tr>
          <td></td>
          <td align="right"><input type="submit"
          name="submit" value="Sent"></td>
        </tr>
        </form>
        </table>
      </td>
    </tr>
</table>
</body>
</html>

Per creare una tabella da riga di comando:

CREATE TABLE person(prodp_id INT NOT NULL AUTO_INCREMENT, nomep VARCHAR(20)NOT NULL, PRIMARY KEY (prodp_id)) ENGINE = InnoDB;

pagina da verificare per inserimento tabella:

ins.html
    <html>
       <head>
          <title>Create a MariaDB Table</title>
       </head>
 
       <body>
          <?php
             $dbhost = 'localhost';
             $dbuser = 'tato';
             $dbpass = 'tatone';
			 $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
 
             if(! $conn ){
                die('Could not connect: ' . mysql_error());
             }
             echo 'Connected successfully<br />';
 
             $sql = "CREATE TABLE products_tbl( ".
                "product_id INT NOT NULL AUTO_INCREMENT, ".
                "product_name VARCHAR(100) NOT NULL, ".
                "product_manufacturer VARCHAR(40) NOT NULL, ".
                "submission_date DATE, ".
                "PRIMARY KEY ( product_id )); ";
 
             mysqli_select_db($conn, 'test' );
             $retval = mysqli_query( $conn,$sql );
 
             if(! $retval ) {
                die('Could not create table: ' . mysql_error());
             }
             echo "Table created successfully\n";
 
             mysqli_close($conn);
          ?>
       </body>
    </html>

Informazioni tipiche per mariadb, andrà creata una pagina apposta:
Quando si crea un database per poterlo gestire da altre interfacce ad esempio php o altre API o altro, è bene creare un utente specifico che abbia i diritti sul database che non sia root per creare tabelle, inserire, modificare e altro. Si fa con il seguente comando che crea anche l'utente appunto.

GRANT ALL PRIVILEGES ON nomedatabase.* TO 'nomeutentechestocreando'@'localhost' IDENTIFIED BY 'passworddelnuovoutente';

in openstack viene creato anche il seguente accesso:

GRANT ALL PRIVILEGES ON nomedatabase.* TO 'nomeutentechestocreando'@'%' IDENTIFIED BY 'passworddelnuovoutente';

ATTENZIONE.
Su internet si trovano ancora un sacco di riferimenti a vecchie versioni di php che usano funzioni ormai dprecate.
Ad esempio mysql_connect() o mysqli_connect()
è bene usare la seconda versione in quanto è più nuova più versatile e recente.
Non so spiegare ancora il perchè ma è così. QUindi evitare tutte le altre guide.

proxmox

ho avuto esperienza di proxmox che dopo aver installato apache e mariadb spembrava installato completamente php invece mancava libapache2-mod-php
sono stato 2 ore a smanettare per poi scoprire che mancava quello. quindi okkio!

software/php.txt · Ultima modifica: 2023/04/17 14:25 (modifica esterna)