Openunix.eu


Web development: PHP

  • To start and close a php script:

    <?php
    echo "here is the html code";
    >

  • To split website to dynamically editable parts:
    <?php include ("website_parts/header.php")>
    <?php include ("website_parts/left_menu.php")>
    here comes the html code
    <?php include ("website_parts/bottom.php")>

  • Session Management in three scripts:
    login.php
    <?php
    echo '<form name="frmUser" method="post" action="login_check.php">';
    echo '<table align="left">';
    echo '<td align="right">login</td>';
    echo '<td><input type="text" name="myusername"></td>';
    echo '</tr>';
    echo '<td align="right">heslo</td>';
    echo '<td><input type="password" name="mypassword"></td>';
    echo '</tr>';
    echo '<td></td><td><input type="submit" name="submit" value="Přihlásit"></td>';
    echo '</tr>';
    echo '</table>';
    ?>
    

    login_check.php
    <?php
    session_start();
    $message="";
    if(count($_POST)>0) {
    
    	require "./conf/connect.php";
    	$myusername=$_POST['myusername'];
    	$mypassword=$_POST['mypassword'];
    	$myusername = mysql_escape_string($myusername);
    	$mypassword = mysql_escape_string($mypassword);
    	
     if ($result = $spojeni->query("SELECT * FROM autori WHERE login='$myusername' and heslo=encrypt('$mypassword',heslo)"))
     {
     	if ($result->num_rows == 1)
    	{
                  while ($row = $result->fetch_object())
                       {
     		// Register session
    	       $_SESSION["user_id"] = $row->id;
                   $_SESSION["user_login"] = $row->login;
                   $_SESSION["user_jmeno"] = $row->jmeno;
    	       echo "Přihlášení úspěšné.";
                   header("Refresh:0; URL=./index.php");
    		   }
    	       }
          else {
    	$_SESSION['errors'] = "Ne, ne, ne. Tak to není... Bude to někde mezi židlí a klávesnicí...";
    
    	         echo $_SESSION['errors'];
                     header("Refresh:0; URL=./index.php");
               } 
      }
      }
    //if(isset($_SESSION["user_id"])) {
    //	header("Location:index.php");
    //}
    ?>
    

    index.php
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <?php
    session_start(); // Starting Session
    $error=''; // Variable To Store Error Message
    
    //////// Here is the page content //////
    if(isset($_SESSION['user_jmeno'])){
    unset($_SESSION["errors"]);
    echo "Your name is:".$_SESSION['user_jmeno']."<;
    echo" <a href='logout.php' tite='Logout'>Odhlásit</a>";
    }
    /////// Here is the page after bad login ///////
    if($_SESSION["errors"]) {
    	echo "</table>";
    	echo "<span style='color:white;'>".$_SESSION['errors']."</span>";
    	require "login.php";
    }
    /////// Here comes the login window ///////
    if((!isset($_SESSION['user_jmeno'])) && (!isset($_SESSION['errors']))){
    	echo "</table>";
    	echo "Nejste přihlášen.<br>";
    	require "login.php";
    }
    ?>
    </html>
    
  •  

    Powered by NetBSD. Running on a toaster.