PHP samples


#1

Does anybody use PHP anymore? Or has Rails taken over? I don't know squat about Rails but I do have some working examples of PHP without a database if anyone is interested.


#2

@mtf I'd be interested in seeing these examples, I plan on working with PHP a bit over the Summer :slight_smile:


#3

Php Is Highly Used. @zystvan @mtf . An Example of PHP's dominant use is obviously Facebook

There are concpiracies like:

"PHP will be dead"

or

"PHP is long gone."

But no. if you look at most of the work that are open source, and websites. a lot are still done in PHP. PHP May be old, but in my oppinon it wont ever go away.. If you guys want to see an example of PHP And Database in work. I have a repo (When i did PHP Long time ago(4 monthish..) ) :

https://github.com/amanuel2/PhpSampleLogin

Let me summarize the PHP Code on the repo if you dont want to look at it:

connection.php:

        <?php
        $host = 'localhost';
        $user = 'root';
        $pass = '';
        $db = 'phpFormTodo';
        $port = 3306;
         $connection = new mysqli($host, $user, $pass, $db, $port);
         if($connection->connect_errno)
           {
               printf("Connect failed: %s\n", $mysqli->connect_error);
               exit();
           }
         ?>

Index.php:

<?php
// Start the session
session_start();
require_once('connection/connection.php')
?>
<!DOCTYPE html>
<html>
<head>
    <script src="node_modules/alertifyjs/build/alertify.js"></script>
    <link rel="stylesheet" href="node_modules/alertifyjs/build/css/alertify.css" />
</head>
<body >
	<h1>Login</h1>
	<form action="" method="POST">
	    <input type="text" placeholder="Username/Email..." name="loginEmail"/><br>
	    <input type="password" placeholder="Password..." name="loginPassword"/><br>
	    <input type="submit" name="submit"/><br>
	    <a href="http://localhost:8012/phpFormTodo/registration/register.php">Or Register</a>
	</form>

	<?php
	    if (isset($_POST['submit'])) {
        $loginEmail = $_POST["loginEmail"];
        $loginPassword = $_POST["loginPassword"];
	         if ($query = mysqli_query($connection, "SELECT * FROM `authe` WHERE Email = '".$loginEmail."' AND Password = '".$loginPassword."' ")) {
	    			$rows = mysqli_num_rows($query);
	    				 	/**
      						 * Successfull Authentication
      						 * User exists because there is a cell
      						 */
		    			 if($rows>0){
						        $_SESSION['email'] = $loginEmail;
						        $_SESSION['password'] = $loginPassword;
						        if(true){
						        	/**
		      						 * QUERY FOR STORING USERNAME IN SESSION
		      						 */
						          if ($queryTwo = mysqli_query($connection, "SELECT Username FROM `authe` WHERE Email = '".$loginEmail."' AND Password = '".$loginPassword."'")) {
						            $rowsTwo = mysqli_num_rows($queryTwo);
						            if($rowsTwo>0){
						              printf($rowsTwo);
						              while($roww = mysqli_fetch_array($queryTwo)){
						                  $_SESSION["username"] =  $roww['Username'];
						                }
						                echo "<script> alertify.success('Sucessfully Authenticated'); </script>";
                            echo "<script> window.location.href = 'http://localhost:8012/phpFormTodo/page/todo.php' </script>";
						            }
						          }
						          else {
						          	echo "<script> alertify.error('Something Went Wrong...'); </script>";
						          }
						        }
      						}
                  /**
                    *Checking if user
                    *put username..
                    */
              else if($query = mysqli_query($connection, "SELECT * FROM `authe` WHERE Username = '".$loginEmail."' AND Password = '".$loginPassword."' ")){
                $rows = mysqli_num_rows($query);
                    /**
                       * Successfull Authentication
                       * User exists because there is a cell
                       *Getting Email..
                       */
                   if($rows>0){
                        $_SESSION['username'] = $loginEmail;
                        $_SESSION['password'] = $loginPassword;
                        if(true){
                          if ($queryTwo = mysqli_query($connection, "SELECT Email FROM `authe` WHERE Username = '".$loginEmail."' AND Password = '".$loginPassword."'")) {
                            $rowsTwo = mysqli_num_rows($queryTwo);
                            if($rowsTwo>0){
                              while($roww = mysqli_fetch_array($queryTwo)){
                                  $_SESSION["email"] =  $roww['Email'];
                                }
                             }
                          }
                          else {
                            echo "<script> alertify.error('Something Went Wrong...'); </script>";
                          }
                        }
                        /**
                          *Getting ID Of User...
                          */
                        if(true){
                          if ($queryID = mysqli_query($connection, "SELECT ID FROM `authe` WHERE Email = '".$loginEmail."' AND Password = '".$loginPassword."'")) {
                            $rowsThree = mysqli_num_rows($queryID);
                            if($rowsThree>0){
                              while($rowww = mysqli_fetch_array($queryID)){
                                  $_SESSION["id"] =  $rowww['ID'];
                                  var_dump($rowww);
                                }
                                echo "<script> alertify.success('Sucessfully Authenticated'); </script>";
                                // echo "<script> window.location.href = 'http://localhost:8012/phpFormTodo/page/todo.php' </script>";
                            }
                            else{
                              echo "<script> alertify.error('Something Went Wrong...'); </script>";
                            }
                          }
                          else {
                            echo "<script> alertify.error('Something Went Wrong...'); </script>";
                          }
                        }
                     }
              }
      						/**
      						 *Not Successfull Authentication
      						 */
					     else {
					       echo "<script> alertify.error('Authetication Failed'); </script>";
					     }
	          }
	    }
	?>

</body>
</html>

Login.php:

    <?php
    require_once('../connection/connection.php')
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <script src="../node_modules/alertifyjs/build/alertify.js"></script>
        <link rel="stylesheet" href="../node_modules/alertifyjs/build/css/alertify.css" />
    </head>
    <body >
    	<h1>Login</h1>
    	<form action="" method="POST">
    	    <input type="username" placeholder = "Username..." name="registerUsername"/>
    	    <input type="email" placeholder="Email..." name="registerEmail"/>
    	    <input type="password" placeholder="Password..." name="registerPassword"/>
    	    <input type="submit" name="submit"/>
    	    <a href="">Or Login</a>
    	</form>

      <?php
     $submit           = $_POST["submit"];
     $registerUsername = $_POST["registerUsername"];
     $registerEmail    = $_POST["registerEmail"];
     $registerPassword = $_POST["registerPassword"];
     if (isset($_POST["submit"])) {
         if ($duplicateCheckQuery = mysqli_query($connection, "SELECT * FROM `authe` WHERE Email = '" . $registerEmail . "' OR  Username = '" . $registerUsername . "'")) {
             $duplicateCheckRows = mysqli_num_rows($duplicateCheckQuery);
             /**
              *Check If Username or Email is
              *Registered.
              */
             if ($duplicateCheckRows > 0) {
                 echo "<script> alertify.error('Username Or/And Email already registered...'); </script>";
             }
             /**
              *Check If Password is min length
              */
             else if (strlen($registerPassword) <= 6) {
                 echo "<script> alertify.warning('Password must at least be 7 characters..'); </script>";
             }
             else {
                 $query = "INSERT INTO authe (authe.Email , authe.Password, authe.Username) VALUES ('" . $registerEmail . "', '" . $registerPassword . "' , '" . $registerUsername . "')";
                 if ($result = $connection->query($query)) {
                     echo "<script>alertify.success('SucessFully Registered! Login Now!');</script>";
                 } else {
                     echo "<script> alertify.error('Registration Failed..'); </script>";
                 }
             }
         }
         else {
             echo "<script> alertify.error('Something Went Wrong...'); </script>";
         }
     }
     ?>

    </body>
    </html>

Register.php

<?php
require_once('../connection/connection.php')
?>
<!DOCTYPE html>
<html>
<head>
    <script src="../node_modules/alertifyjs/build/alertify.js"></script>
    <link rel="stylesheet" href="../node_modules/alertifyjs/build/css/alertify.css" />
</head>
<body >
	<h1>Login</h1>
	<form action="" method="POST">
	    <input type="username" placeholder = "Username..." name="registerUsername"/>
	    <input type="email" placeholder="Email..." name="registerEmail"/>
	    <input type="password" placeholder="Password..." name="registerPassword"/>
	    <input type="submit" name="submit"/>
	    <a href="">Or Login</a>
	</form>

  <?php
 $submit           = $_POST["submit"];
 $registerUsername = $_POST["registerUsername"];
 $registerEmail    = $_POST["registerEmail"];
 $registerPassword = $_POST["registerPassword"];
 if (isset($_POST["submit"])) {
     if ($duplicateCheckQuery = mysqli_query($connection, "SELECT * FROM `authe` WHERE Email = '" . $registerEmail . "' OR  Username = '" . $registerUsername . "'")) {
         $duplicateCheckRows = mysqli_num_rows($duplicateCheckQuery);
         /**
          *Check If Username or Email is
          *Registered.
          */
         if ($duplicateCheckRows > 0) {
             echo "<script> alertify.error('Username Or/And Email already registered...'); </script>";
         }
         /**
          *Check If Password is min length
          */
         else if (strlen($registerPassword) <= 6) {
             echo "<script> alertify.warning('Password must at least be 7 characters..'); </script>";
         }
         else {
             $query = "INSERT INTO authe (authe.Email , authe.Password, authe.Username) VALUES ('" . $registerEmail . "', '" . $registerPassword . "' , '" . $registerUsername . "')";
             if ($result = $connection->query($query)) {
                 echo "<script>alertify.success('SucessFully Registered! Login Now!');</script>";
             } else {
                 echo "<script> alertify.error('Registration Failed..'); </script>";
             }
         }
     }
     else {
         echo "<script> alertify.error('Something Went Wrong...'); </script>";
     }
 }
 ?>

</body>
</html>

#4

I have to root around in a bunch of old drives. Here's an old page that self loads a thumbnail page and generates links to the full size image. It uses lightbox_plus, but doesn't have to. Just more self contained and no page loads.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Self loading thumbnail page</title>
<meta name="X-Resource-Dir" content="/resource/" />
<meta name="X-Script-Dir" content="/resource/" />
<link href="/resource/lightbox.css" rel="stylesheet" type="text/css" media="screen" />
<script src="/resource/spica.js" type="text/javascript"></script>
<script src="/resource/lightbox_plus.js" type="text/javascript"></script>
<style type="text/css">
body {
 background-image: url(/resource/overlay.png);
    background-repeat: repeat;
}
img {
 margin: 2px 10px;
 vertical-align: middle;
}
p {
text-align: center;
font-weight: bold;
}
</style>
</head>
<body>
<div>
<?php
if ($handle = opendir('.') )
{
while (false !== ($file = readdir($handle) ))
 { 
if ($file != "." && $file != ".." && $file != "index.php")
  {
if (stristr($file,'.jpg'))
   {
 $endflag=strpos($file,'_');
 $refnum=substr($file,0,$endflag);
echo "<a href=\"$file\" rel=\"lightbox1\" ><img src=\"$file\" width=\"100\" title=\"$refnum\" alt=\"$refnum\" /></a>\n"; 
   }
else
   {
echo "<br /><a href='$file' >$file</a><br />\n"; 
   }
  }
 }
}
?>
</div>
<p>page footer</p>
</body>
</html>

I haven't checked under the hood of this program, but I believe it may be the same, only newer. The version I'm running on this page is from circa 2010, probably earlier.

http://serennz.sakura.ne.jp/toybox/lightbox/

Of course, this was from a time when I didn't know jQuery or AngularJS. There are probably some great lightbox scripts out there.

All you need is a folder of picture files on a web server or in your local php environment (this script only looks for .jpg but you can expand on that) and drop this file in with an index.php file name. Be sure there isn't an index.html page present or this one might not load. Better if the folder contains only pictures.

Navigate to the URL in your browser.

Notes.

 $endflag=strpos($file,'_');
 $refnum=substr($file,0,$endflag);

The picture files in this example have camera file names ("P5171591") followed by an underscore followed by dimensions. These lines extract everything up to the underscore and insert this in the title="" and alt="" attributes.