PHP – Simple Password Protection – No Sessions

PHP – Simple Password Protection – No Sessions

Take care of yourself! Read the comments inside!

DOWNLOAD

 

SELF PAGE PROTECTION


<html>
<body>
<!-- SIMPLE PHP PASSWORD PROTECTION WITH NO SESSIONS -->
<!-- Author: blog.lucedigitale.com - Andrea Tonin -->

<!-- FORM START -->
<!-- Il form invia a se stesso PHP_SELF la password con il nome 'password' -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Type Password: <input type="text" name="password">
<input type="submit">
</form>
<!-- FORM END -->

<?php 
// $_REQUEST colleziona il dato ricevuto dal form 
$password = $_REQUEST['password']; 
// se la password è corretta visualizza il contenuto HTML nascosto 
if ($password=="test")
  {
  echo "
  <!-- SECRET CONTENT START -->
  Password OK!<br>
  <strong>This is my super HTML formatted secret content!</strong>
  <!-- SECRET CONTENT END -->
  ";
  }
  else
  {
  echo "You need password to enter";
  }
?>

</body>
</html>

SPLIT PAGES PROTECTION

You must create in the same folder:
1. password-form.php -> the password form
2. password-reserved.php -> the password protected page

The code for password-form.php

<html>
<body>
<!-- SIMPLE PHP PASSWORD PROTECTION WITH NO SESSIONS -->
<!-- Author: blog.lucedigitale.com - Andrea Tonin -->

<!-- FORM START -->
<!-- Il form invia a password-reserved.php la password con il nome 'password' -->
<form method="post" action="password-reserved.php">
Type Password: <input type="text" name="password">
<input type="submit">
</form>
The password is test
<!-- FORM END -->
</body>
</html>

The code for password-reserved.php

<?php 
// $_REQUEST colleziona il dato ricevuto dal form 
$password = $_REQUEST['password']; 

if ($password=="test")
  // se la password è corretta visualizza il contenuto HTML nascosto 
  {
  echo "
  <!-- SECRET CONTENT START -->
  <html>
  <body>
  Password OK!<br>
  <strong>This is my super HTML formatted secret content!</strong>
  
  </body>
  </html>
  <!-- SECRET CONTENT END -->
  ";
  }
  else
  {
  // se la password non è corretta visualizza il messaggio di errore
  echo "You need password to enter";
  }
?>

SPLIT PAGES PROTECTION + Redirection JavaScript

The code for password-form.php

<html>
<body>
<!-- SIMPLE PHP PASSWORD PROTECTION WITH NO SESSIONS -->
<!-- Author: blog.lucedigitale.com - Andrea Tonin -->

<!-- FORM START -->
<!-- Il form invia a password-reserved.php la password con il nome 'password' -->
<form method="post" action="password-reserved.php">
Type Password: <input type="text" name="password">
<input type="submit">
</form>
The password is test
<!-- FORM END -->
</body>
</html>

The code for password-reserved.php

<?php 
// $_REQUEST colleziona il dato ricevuto dal form 
$password = $_REQUEST['password']; 

if ($password=="test")
  // se la password è corretta visualizza il contenuto HTML nascosto 
  {
  echo "<center>Password Corretta</center>";
  }
  else
  {
  // se la password è errata visualizza il messaggio di errore e torna al login
  echo "<center>Password Errata</center> <script>location.href = 'password-form.php';</script>";
  }
?>

<!DOCTYPE html>
... Your Secret Content ...
</html>
By |PHP, Web Design|Commenti disabilitati su PHP – Simple Password Protection – No Sessions

PHP – Simple Email Sender

PHP – Email Sender

DOWNLOAD

 

You must create in the same folder 2 files:
1. email-form.php -> the email form
2. email-sender.php -> the PHP email sender engine

Simplest Email Sender – No Validation and Security controls

email-form.php

<html>
<body>
<!-- SIMPLE PHP EMAIL SENDER -->
<!-- Author: blog.lucedigitale.com - Andrea Tonin -->

<!-- FORM START -->
<!-- Il form invia a email-sender.php tutti i dati raccolti -->
<form method="post" action="email-sender.php">
Name: 
<br>
<input type="text" name="name">
<br>
E-mail: 
<br>
<input type="text" name="email">
<br>
Comment: 
<br>
<textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
<input type="submit">
</form>
<!-- FORM END -->

</body>
</html>

email-sender.php

<?php 

// SIMPLE PHP EMAIL SENDER 
// Author: blog.lucedigitale.com - Andrea Tonin 

// EDIT THE 2 LINES BELOW AS REQUIRED
// email a cui inviare il messaggio e il suo soggetto
$email_to = "andreat@lucedigitale.com";
$email_subject = "New email from your website";

// $_REQUEST colleziona il dato ricevuto dal form 
$email_name = $_REQUEST['name']; 
$email_from = $_REQUEST['email']; 
$email_comment = $_REQUEST['comment']; 

$email_content = "\n Name: ".$email_name."\n Email: ".$email_from."\n Comment: \n"."\n".$email_comment;

// Create email headers
// Invio l'email all'indirizzo specificato nella variabile $email_to specificata all'inizio del codice
$headers = 'From: '.$email_name."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_content, $headers); 

// Messaggio di avvenuta spedizione, si arriva a questa porzione di codice solo se la parte precedente dello script ha funzionato correttamente
echo "Thanks! The mail has been sent successfully!";

?>
By |PHP, Web Design|Commenti disabilitati su PHP – Simple Email Sender

PHP – MYSQL – Insert Records

PHP – MYSQL – Insert Records

I Records sono le righe della tabella.

Syntax:

INSERT INTO table_name
VALUES (value1, value2, value3,…)

or

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
// Aggiungere nuovi record alle colonne - START
// Statement:
// INSERT INTO table_name (column1, column2, column3,...) 
// VALUES (value1, value2, value3,...)
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");

mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire',33)");
echo "Great! New Record Inserted!"; 
// Aggiungere nuovi record alle colonne - END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin Dashboard:

mysql-0003

By |MySQL, PHP, Web Design|Commenti disabilitati su PHP – MYSQL – Insert Records

PHP – MYSQL – Create a Table

PHP – MYSQL – Create a Table

1. You can create a table using phpMyAdmin

Blue Host users:

CPanel> phpMyAdmin> Enter with User name and password

LEFT COLUMN> you will see ‘Create Table’ button

2. You can create a table using PHP script

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
  
// Create table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Execute the script and check the Database with phpMyAdmin, you will see on TOP LEFT a small icon with a GREEN ARROW that will refreshes the table list.

Click over the new Table ‘Persons’, you must see:

– a table named “Persons”
– three columns: “FirstName” – “LastName” – “Age”

AN ERROR MESSAGE: Nessun indice definito!

Ogni tabella necessita di un indice UNIVOCO per identificare le righe in una tabella.
Per creare una tabella con un indice possiamo utilizzare il seguente codice:

$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

Eliminiamo la tabella da phpMyAdmin:
phpMyAdmin> In ALTO localhost> lucedigi_testphp> spuntiamo la tabella> ‘Elimina’ il messaggio di conferma sarà “Drop Table…”> OK

Eseguiamo lo script:

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
  
// Create table with an INDEX
// PID is: Primary Key ID
// it is an INT integer number
// it is not NULL
// AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added.
$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table Persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Controllare il database with phpMyAdmin: il messaggio di errore è scomparso, sotto è stata aggiunta la voce ‘Indice’ dove la chiave primaria è il ‘Campo’ ‘PID’

By |MySQL, PHP, Web Design|Commenti disabilitati su PHP – MYSQL – Create a Table

PHP – Global Variables – Superglobals

PHP – Global Variables – Superglobals

Superglobals are variables that are always available in all scopes.
Superglobals always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

The PHP superglobal variables are:

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

$GLOBAL

$GLOBAL is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

In the next example, since z is a variable present within the $GLOBALS array, it is also accessible form outside the function!

<?php 

$x = 75;
$y = 25; 

function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z; // Output: 100

?>
By |PHP, Web Design|Commenti disabilitati su PHP – Global Variables – Superglobals