programmazione

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 – Functions

PHP – Functions

Remember that function names are case-insensitive

Basic Structure

<?php
function writeMsg()
{
echo "Hello World!";
}

writeMsg(); // call the function and output: Hello World!
?>

Arguments

Variables can be passed to functions through arguments.

<?php

// The argument is: $fname 
function familyName($fname)
{
echo "$fname Tonin<br>";
}

familyName("Andrea");    // Output: Andrea Tonin
familyName("Erica");     // Output: Erica Tonin
familyName("Antonio");   // Output: Antonio Tonin
familyName("Maria");     // Output: Maria Tonin
familyName("Alice");     // Output: Alice Tonin

?>
<?php
function familyName($fname,$year)
{
echo "$fname Tonin. Born in $year <br>";
}

familyName("Andrea","1974");    // Output: Andrea Tonin. Born in 1974 
familyName("Erica","1975");     // Output: Erica Tonin. Born in 1975 
familyName("Serafina","1954");  // Output: Serafina Tonin. Born in 1954 
?>
<?php
function setHeight($minheight=50)
{
echo "The height is : $minheight <br>";
}

setHeight(350); // Output: The height is : 350 
setHeight();    // Output: The height is : 50   -> default value
setHeight(135); // Output: The height is : 135
setHeight(80);  // Output: The height is : 80 
?>
<?php
function sum($x,$y)
{
$z=$x+$y;
return $z;
}

echo "5 + 10 = " . sum(5,10) . "<br>"; // Output: 5 + 10 = 15
echo "7 + 13 = " . sum(7,13) . "<br>"; // Output: 7 + 13 = 20
echo "2 + 4 = " . sum(2,4);            // Output: 2 + 4 = 6
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Functions

PHP – Loops – while – do…while – for – foreach

PHP – Loops – while – do…while – for – foreach

while

<?php 
$x=1; 
while($x<=5) // It executes the code as long as the specified condition is true.
  {
  echo "The number is: $x <br>";
  $x++;
  } 
?>

do…while

<?php 
$x=1; 
do
  {
  echo "The number is: $x <br>";
  $x++;
  }
while ($x<=5)  // It executes the code as long as the specified condition is true.
?>

NOTICE:
while -> 1. condition verification 2. code execution
do…while -> 1. code execution 2. condition verification

for

<?php 
for ($x=0; $x<=10; $x++)
  {
  echo "The number is: $x <br>";
  } 
?>

foreach

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

<?php 
$colors = array("red","green","blue","yellow"); 
foreach ($colors as $value)
  {
  echo "$value <br>";
  }
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Loops – while – do…while – for – foreach

PHP – If Else

PHP – If Else

<?php

// -----------
// if
// -----------

$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }

// -----------
// if ... else
// -----------

$t=date("H");
if ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }

// -----------
// if ... elseif ... else
// -----------

$t=date("H");
if ($t<"10")
  {
  echo "Have a good morning!";
  }
elseif ($t<"20")
  {
  echo "Have a good day!";
  }
else
  {
  echo "Have a good night!";
  }

?>
By |PHP, Web Design|Commenti disabilitati su PHP – If Else

PHP – String Manipulation

PHP – String Manipulation

The most used functions for String Manipulation:

<?php

// ---------------------------------------------------------------

// String Operators

$a = "Hello";
$b = $a . " world!"; // Concatenation
echo $b; // outputs Hello world! 

$x="Hello";
$x .= " world!"; // Concatenation Assignement
echo $x; // outputs Hello world!

// ---------------------------------------------------------------

// strlen - To return the lenght of the string, blank spaces included 

echo strlen("Hello world!"); // the result is - 12 -

// ---------------------------------------------------------------

// strpos - To search for a specified character or text within a string.

// If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.
// The first character position in the string is 0, and not 1.

echo strpos("Hello world!","world"); // the result is 6

// ---------------------------------------------------------------

// trim - Strip (Remove) whitespace (or other characters) from the beginning and end of a string

$text   = "Hello World";
echo $text;                 // Output: Hello World
echo '<br>';
echo trim($text, "Hello");  // Output: World
echo trim($text, "Hd");     // Output: ello Worl

// str_replace - Replace all occurrences of the search string with the replacement string

$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");
echo $newphrase = str_replace($healthy, $yummy, $phrase);
                            // Output: You should eat pizza, beer, and ice cream every day

// ---------------------------------------------------------------

// substr - Returns the portion of string specified by the start and length parameters.

echo substr("abcdef", -1);    // Output: "f"
echo substr("abcdef", -2);    // Output: "ef"
echo substr("abcdef", -3, 1); // Output: "d"

// ---------------------------------------------------------------

// stripslashes - Remove backslashes

$str = "Is your name O\'reilly?";
echo stripslashes($str);    // Output: Is your name O'reilly?

// ---------------------------------------------------------------

// strip_tags - Remove HTML and PHP tags

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo $text;                 // Output HTML formatted text
echo strip_tags($text);     // Output plain text

// ---------------------------------------------------------------

// strtolower - Make a string lowercase

$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Output: mary had a little lamb and she loved it so

// ---------------------------------------------------------------

?>  

All string functions at: http://www.php.net/manual/en/ref.strings.php

By |PHP, Web Design|Commenti disabilitati su PHP – String Manipulation