Things About Web Design Logo

Things About Web Design

PHP CSS MySQL JS Scripts & Tutorials

PHP Snippets


.

PHP Random Password Maker

The HTML and PHP coding in box below generates strong passwords:

Password Generator
Password Length:
Include Numbers
Include Special Characters

16

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>#13" method="POST"> <i style="color:Crimson;"><b>Password Generator</b></i><br /> Password Length: <input type="number" name="length" min="4" max="16" value="12"><br> <input type="checkbox" name="nums" value="nums" checked>Include Numbers<br> <input type="checkbox" name="special" value="special" checked>Include Special Characters<br> <input type="submit" name="sub" value="Generate"> </form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if(isset(
$_POST['nums']) && isset($_POST['special']))
    
$string 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789!#$@%&()_+';
    else if (isset(
$_POST['nums'])) $string 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789';
    else if (isset(
$_POST['special'])) $string 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ!#$@%&()_+';
    else 
$string 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
    
$length $_POST['length'];
    
$password "";
    for (
$i0$i<$length;$i++){
        
$password $password.$string[mt_rand(0strlen($string)-1)];
}
    echo 
$password;
}
  
?>
.

PHP Header Location

The PHP coding in box below redirects to a URL:

15


<?php header("Location: current.php"); ?>

<?php header("Location: http://www.example.com/");?>

<?php
  header
("Refresh: 5; url=http://www.example.com"); //will redirect after 5 seconds
?>
.

PHP Server And Visitor Info

The PHP code in box below returns server and visitor information:

14


<?php
echo $_SERVER['PHP_SELF'];
echo 
"<br>";
echo 
$_SERVER['SERVER_NAME'];
echo 
"<br>";
echo 
$_SERVER['HTTP_HOST'];
echo 
"<br>";
echo 
$_SERVER['HTTP_REFERER'];
echo 
"<br>";
echo 
$_SERVER['HTTP_USER_AGENT'];
echo 
"<br>";
echo 
$_SERVER['SCRIPT_NAME'];
?>
.

PHP Page Last Updated

The PHP code returns the last date page updated:

13


<?php
$file 
$_SERVER["SCRIPT_NAME"];
$break Explode('/'$file);
$pfile $break[count($break) - 1];
//echo $pfile;
echo "<small style=font-size:10px>";
echo 
"Page Last Updated " .date("M j, Y",filemtime($pfile));
echo 
"</small>";
?>
.

PHP Visitor Zip File Download

The 'PHP' code below allows for users to download a zip file:

12

<div class='container'> <h1>Create and Download Zip file using PHP</h1> <form method='post' action='thumbs.zip'> <!--input type='submit' name='create' value='Create Zip' /-->  <input type='submit' name='download' value='Download'/> </form> </div> <a type="submit" href="thumbs.zip" download>Download</a>  
<?php
//Create an object from the ZipArchive class.
$zipArchive = new ZipArchive();
 
//The full path to where we want to save the zip file.
$zipFilePath 'thumbs.zip';
 
//Call the open function.
$status $zipArchive->open($zipFilePath,  ZipArchive::CREATE);
 
 
//An array of files that we want to add to our zip archive.
//You should list the full path to each file.
$filesToAdd = array(
    
'blogthumbs/thumb-1.jpg',
    
'blogthumbs/thumb-2.jpg',
    
'blogthumbs/thumb-3.jpg',
    
'blogthumbs/thumb-4.jpg',
    
'blogthumbs/thumb-5.jpg',
    
'blogthumbs/thumb-6.jpg',
    
'blogthumbs/thumb-7.jpg',
    
'blogthumbs/thumb-8.jpg',
    
'blogthumbs/thumb-9.jpg',
    
'blogthumbs/thumb-10.jpg',
    
'blogthumbs/thumb-11.jpg',
    
'blogthumbs/thumb-12.jpg'
);
 
//Add our files to the archive by using the addFile function.
foreach($filesToAdd as $fileToAdd){
    
//Add the file in question using the addFile function.
    
$zipArchive->addFile($fileToAdd);
}
 
//Finally, close the active archive.
$zipArchive->close();
//exit;
?>
.

PHP Show All Images In Folder

The 'PHP' code below uses glob to get all image files from a directory use (*.*). Use (*.jpg) to only get .jpg files. Good for .jpg .png .gif or any extension:

11


<?php
$dir_name 
"path/to/image/folder/";
$images glob($dir_name."*.*");
foreach(
$images as $image) {
   echo 
"<img src="" /><br />";
}
?>
.

PHP Write Visitor ip To File

The PHP code below gets the website visitors ip address and writes the date and ip to "visitors.txt":

10


<?php
$line 
date('M j Y g:i:s a T ') . " - $_SERVER[REMOTE_ADDR] - $_SERVER[REQUEST_URI]";
file_put_contents('visitors.txt'$line PHP_EOLFILE_APPEND);
?>
.

PHP Form HtmlSpecialChars

The highlighted code below is used in your contact form PHP to remove html tags (security):

9


<form method="post" action="<?php echo htmlspecialchars("sendmail.php");?>"></form>

PHP Footer Copyright Year

The code below is an auto copyright update for the footer in websites: see php.net for more.

8


© Copyright 2017-<?php echo date("Y");?>
.

PHP Show Source

The codes below can be used to return source code or PHP in external web files:

7


<?php show_source("page.php");?>
      
  
<?php highlight_file("page.php");?>
.

PHP Highlight File

The top code in box below returns highlited code like the lower result:

6

<?php highlight_string('<?php $var = "This is a string."; echo $var;?>');?> returns: <?php $var "This is a string."; echo $var;?>
.

PHP All Text File Lines

The code below gets all lines from $url.txt file in directory "snips" and makes a link list of them:

5


<?php $url="https://" htmlspecialchars($_SERVER["REQUEST_URI"],ENT_QUOTES"UTF-8"); $url=str_replace(array($query,"https://","http://","www.","/","yoursite.com","code",".php"),\, $url);?>
<!-- 2 Card -->
<div class="rtcontainer">
<div class="rightbg">
<h4 class="glow"><a href="<?php echo "$url.php"?>" title="<?php echo "$url"?>"><?php echo "$url"?></a></h4>
</div>
<?php
// get current directory path
$dirpath "snips/*$url.txt";

// copy filenames to array
$files = array();
$files glob($dirpath);

foreach(
$files as $item){
    
$all_lines file($item);
    
$file1 "$item";
$lines file($file1);
foreach(
$lines as $line_num => $line){
          
$link str_replace(" ""-"$line);
            echo 
"<a href=$url.php#$link>$line</a><br />";
    }
}
?>
.

PHP Specific Text File Lines

The code below gets lines [0] and [2] from every .txt file in directory "comments":

4


<?php
// get current directory path
$dirpath "comments/*.txt";

// copy filenames to array
$files = array();
$files glob($dirpath);

// sort files by last modified date
usort($files, function($x$y) {
    return 
filemtime($x) < filemtime($y);
});

foreach(
$files as $item){
             
// echo basename($item) . " => Posted " . @date("F d, Y, H:i:s", filemtime($item)) . "<br/>";

      
$all_lines file($item);
            echo 
"<h5>";
            echo 
"<span>ok $all_lines[0]</span>";
            echo 
"</h5>";
            echo  @
date($date"F d, Y, g:i a T"filemtime($item)) . "<br/>";
            echo 
"<p>";
            echo 
$all_lines[2];
            echo 
"</p>";
}
?>
.

PHP Server Date Time Zone

The code below returns Server Pacific Time (PST): March 01, 2021, 4:59 pm PST

3


<?php $zone date("F d, Y, g:i a T",strtotime($date" " .$timezone)); 
echo 
$zone?>
.

PHP Local Date Time Zone

The code below returns Local Mountain Time (MST): March 01, 2021, 5:59 pm MST

2


<?php
$zone 
date_create(""timezone_open("America/Denver"));
date_timezone_set($zonetimezone_open("America/Denver"));
echo 
date_format($zone"F d, Y, g:i a T") . "\n";
?>

PHP Sanitized Back Link

Page back like two examples above. But with text link. And sanitized http referrer: back

1


<?php
  $url 
htmlspecialchars($_SERVER[HTTP_REFERER]);
  echo 
"<a href=$url>back</a>"
?>