簡體   English   中英

登錄和注冊php程序

[英]Login and Registration php program

我想知道將代碼放置在什么位置/我的checkLogin.php,以便在單擊“注冊”時將其帶到注冊表單。 我不確定應該在哪里放置代碼或應該使用什么命令制作代碼,以便單擊注冊按鈕時,它不會告訴我我沒有填寫用戶名或密碼。

checkLogin.php:

<?php
/* connection info */
ini_set("display_errors","on");
error_reporting(E_ALL | E_STRICT);
ini_set("include_path","../../includes");
include("dbinfo.inc");
$cxn = mysqli_connect($host,$user,$password,$dbname) 
    or die("Couldn't connect to server.");

?>
<?php
/*  Program name: checkLogin.php 
 *  Description:  Program displays the blank form and checks 
 *  all the form fields for blank fields.
 */
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
{
  foreach($_POST as $field => $value)           
  {
    if(empty($value))
      {
         $blank_array[] = $field;
      }
    else                                                    
    {
      $good_data[$field] = strip_tags(trim($value));
    }
  } 

  if(@sizeof($blank_array) > 0) 
  {
    $message = "<p style='color: red; margin-bottom: 0; 
                 font-weight: bold'>
                 You didn't fill in one or more required fields. 
                 You must enter: 
                 <ul style='color: red; margin-top: 0; 
                 list-style: none' >";

 /* display list of missing information */
    foreach($blank_array as $value)
    {
       $message .= "<li>$value</li>";
    }
    $message .= "</ul>"; 

    echo $message;
    extract($good_data);
    include("login.inc");   
    exit();    
  }

  foreach($_POST as $field => $value)
{
  if(!empty($value))
  {
    $user_patt = "/^[A-Za-z0-9]{8,20}$/";
    $pass_patt = "/^[A-Za-z0-9*!@$&]{8,12}$/";

    if(preg_match("/user/i",$field))
    {
      if(!preg_match($user_patt,$value))
      {
        $error_array[] = "$value is an invalid $field";
      }
    }

    if (preg_match("/pass/i",$field))
    {
        if(!preg_match($pass_patt, $value))
        {   
            $error_array[] = "Invalid $field";  
        }
     }
  } 

  $clean_data[$field] = strip_tags(trim($value));

}

if(@sizeof($error_array) > 0) 
{

  $message = "<ul style='color: red; list-style: none' >";
  foreach($error_array as $value)
  {
   $message .= "<li>$value</li>";
  }
  $message .= "</ul>"; 

  echo $message;
  extract($clean_data);
  include("login.inc");
  exit();   
}
 else
    {
    foreach($good_data as $field => $value)
        {
            $clean_data[$field] = mysqli_real_escape_string($cxn,$value);
        }
    $sql = "SELECT * from UserInfo where username='$clean_data[username]' and
        password='$clean_data[password]'";
        $result = mysqli_query($cxn,$sql) or die("<p>Couldn't connect to login server</p>");
        $row = mysqli_fetch_assoc($result);
        if ($row > 0)
        {
            $sql2 = "UPDATE TimeStamp SET time = CURRENT_TIMESTAMP where username='$clean_data[username]'";  
               $result2 = mysqli_query($cxn,$sql2) or die("<p>Couldn't connect to login server</p>");
            include('goodLogin.inc');
        }
        else
        {
            echo $message;
            extract($clean_data);
            include('login.inc');
            exit();
        } 
    }   
}
else
{
  include("login.inc");
}   
?>

login.inc:

<?php
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
    echo "<h2>Login Form</h2>";

foreach ( $labels as $field => $label)

    if($field != "password")
    {
        echo "<div class='field'>
                <label for='$field'>$label</label>
                    <input id='$field' name='$field' type='text' value='".@$$field."'
                        size='42' /></div>";
    } 
        else 
                {
                    echo "<div class='field'>
                        <label for='$field'>$label</label>
                        <input id='$field' name='$field' type='password' maxlength='20' value='".@$$field."' size='42' />";
                    echo "</div>";
                }

        echo "<div id='submit'>
            <input type='hidden' name='submitted' value='yes'>
            <input type='submit' value='$login'></div>
            <h4>If you are new click register</h4>";

        echo "<div id='register'>
            <input type='submit' value='$register' a href='register.php'></div>";
        echo "</form>";
?>

Register.php:

<?php
/* Program name: form_inc
 * Description:  Defines the Form to display a user's information
 */
$labels = array (
    "username" => "Username",
    "password" => "Password",
    "first_name" => "First Name",
    "last_name" => "Last Name",
    "city" => "City",
    "email" => "E-mail",);

$country = array (
    "select" => "",
    "us" => "United States",
    "ca" => "Canada",
    "mx" => "Mexico", );    

$register = "Register";
?>
<html>
<head>
<meta charset="utf-8">
<title>Exercise 3 Order Form</title>
<style type=>
    form {
        margin: 5em auto 8em;
        padding: 0; 
    }
    h2 {
        text-align: center; 
    }
    form {
        float: left;
        width: 450px;
        padding-left: 20px;
        padding-bottom: 10px;
        border: solid 2px #CCC;
        text-align: left;
    }
    .field {
        padding-top: .5em;
        padding-botton: 2em;    
    }
    label {
        font-weight: bold;
        float: left;
        width: 20%;
        margin-right: 1em;
        text-align: right;
    }   
    #submit {
        float: left;
        margin-top: 4%;
        margin-left: 2%;    
    }
    #register {
        float: right;
        margin-top: 2%;
        margin-right: 2.5%; 
    }
    h4 {
        margin-left: 42%;   
    }
</style>
</head>

<body>

<?php
    echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
    echo "<h2>Registration Form</h2>";
    foreach ( $labels as $field => $label)
    {
        echo "<div class='field'>
                <label for='$field'>$label</label>
                    <input id='$field' name='$field' type='text' value='".@$$field."'
                        size='42' /></div>";
        if($field == "city") {
            echo "<div class='field'>";

            echo "<label for='country'>Country</label>
                    <select id='country' name='country'>";

                foreach ( $country as $select => $option)
                {
                    echo "<option value='$select'>$option</option>";
                }

            echo "</select></div>";
        }
    }
        echo "<div id='register'>
            <input type='submit' value='$register'></div>";
        echo "</form>";
?>

嘗試將整個登錄或注冊系統寫入一個文件,或者最好將其寫入命名空間:

namespace myfile;
class Membership {
     public function exists() { returns true/false; }
     public function signup() { returns true/false; }
     public function signin() { returns true/false; }
}

並通過以下方式連接到您的名稱空間:

require 'membership.php';
use Membership as ms;
$obj = new ms\membership ();
$obj->signup(array('username'=>'' ,'password' => ''));

並與您的庫一起使用,而不是分離。

我要在這里暗中刺傷,但是您有注冊按鈕,我們假裝它看起來像這樣:

<a href="mylogin.php?register">Register</a>

請參閱如何將?register部分添加到URL?

然后,您可以執行以下操作:

include isset($_GET['register']) ? "register.inc" : "login.inc";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM