1

嘿,伙计们,我正在创建一个带有登录和注册的网站,并且默认情况下会在用户在表单中注册然后登录后创建一个个人资料页面帽子这些信息将显示为个人资料页面中的个人资料信息,每个人都是唯一的用户。用户名将指定每个用户及其个人资料页面。这些是一些 php 文件,任何人都知道如何仅显示属于指定用户的信息,而不是所有用户的所有信息。

注册表格.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration Page</title>
<link href="style/stylesheet.css" rel="stylesheet" type="text/css" />

</head>

<?php require_once('header.php'); ?>


<body>

<h2 class="RegisterTitleForm">Registration Form</h2>
<h3 class="requiredField">* Requierd Field!!</h3>
<table width="280" border="0" align="center">
<form action="registerProcess.php" method="post" id="registerForm">
  <tr>
    <input type="hidden" name="uid" id="uid" />
    <td style="text-align: right"><label for="firstname"><span class="Fields">First Name</span> <span class="requiredField">*</span></label></td>
    <td><input type="text" name="firstname" id="firstname" /></td>
  </tr>
  <tr>
    <td style="text-align: right"><label for="lasttname" class="Fields">Last Name</label></td>
    <td><input type="text" name="lastname" id="lastname" /></td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="birthdate">Birth Date</label></td>
    <td><input type="date" name="birthdate" value= "YYYY_MM_DD"  onfocus="if (this.value == 'YYYY_MM_DD') {this.value = '';}" onblur="if (this.value == '') {this.value = 'YYYY_MM_DD';}"  type="text" id="birthdate" /></td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="phonenumber">Phone Number</label></td>
    <td><input type="tel" name="phonenumber" value="000-0-000 000" onfocus="if (this.value == '000-0-000 000') {this.value = '';}" onblur="if (this.value == '') {this.value = '000-0-000 000';}"  type="text"  id="phonenumber" /></td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="gender">Gender <span class="requiredField">*</span></label></td>
    <td><p>
      <label class="Fields">
        <input type="radio" name="genderGroup" value="Male" id="genderGroup_male" />
        Male</label>
      <br />
      <label class="Fields">
        <input type="radio" name="genderGroup" value="Female" id="genderGroup_female" />
        Female</label>
      <br />
    </p></td>
  </tr>
   <tr>
    <td class="Fields" style="text-align: right"><label for="country">Country</label></td>
    <td><select name="country" id="country"><option selected=>please choose coutry<option>lebanon<option>Us<option>europe 
    </select></td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="adress">Local Adress <span class="requiredField">*</span></label></td>
    <td><input type="text" name="adress" id="adress" /></td>
  </tr>

  <tr>
    <td class="Fields" style="text-align: right"><label for="specialisation">Specialisation <span class="requiredField">*</span></label></td>
    <td><select name="specialisation" id="specialisation">
    </select></td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="email">Email Adress<span class="requiredField">*</span></label></td>
    <td><input type="email" name="email" id="email" /></td>
</td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="username">User Name<span class="requiredField">*</span></label></td>
    <td><input type="text" name="username" id="username" /></td>
</td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="password">Password<span class="requiredField">*</span></label></td>
    <td><input type="password" name="password" id="password" /></td>
</td>
  </tr>
  <tr>
    <td class="Fields" style="text-align: right"><label for="password2">Re_Password<span class="requiredField">*</span></label></td>
    <td><input type="password" name="password2" id="password2" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="register" id="register" value="Register" /></td>
  </tr>
  </form>
</table>

</body>
</html>

注册进程.php

<?php
  require_once('config.php'); 

  if(isset($_POST['register']))
{
  if(! get_magic_quotes_gpc() )
  {
   $firstname = addslashes ($_POST['firstname']);
   $lastname = addslashes ($_POST['lastname']);
   $birthdate = addslashes ($_POST['birthdate']);
   $phonenumber = addslashes ($_POST['phonenumber']);
   $genderGroup = addslashes ($_POST['genderGroup']);
   $country = addslashes ($_POST['country']);
   $adress = addslashes ($_POST['adress']);
   $specialisation = addslashes ($_POST['specialisation']);
   $email = addslashes ($_POST['email']);
   $password2 = addslashes ($_POST['password2']);
   $username = addslashes ($_POST['username']);
   $password = addslashes ($_POST['password']);
   $password2 = addslashes ($_POST['password2']);
  }
  else
  {
   $firstname = $_POST['firstname'];
   $lastname = $_POST['lastname'];
   $birthdate = $_POST['birthdate'];
   $phonenumber = $_POST['phonenumber'];
   $genderGroup = $_POST['genderGroup'];
   $country = $_POST['country'];
   $adress = $_POST['adress'];
   $specialisation = $_POST['specialisation'];
   $email = $_POST['email'];
   $username = $_POST['username'];
   $password = $_POST['password'];
   $password2 = $_POST['password2'];

  }


  $sql = "INSERT INTO users ".
       "(firstname,lastname, birthdate, phonenumber, gender, country, localadress, specialisation, email, username, password, password2, joindate) ".      "VALUES('$firstname','$lastname','$birthdate','$phonenumber','$genderGroup','$country','$adress','$specialisation','$email','$username','$password','$password2', NOW())";

  $retval = mysql_query( $sql, $conn );
  if(! $retval )
  {
   die('Could not enter data: ' . mysql_error());
  }
  echo "Entered data successfully\n";

}
  mysql_close($conn);
?>

登录表单.php

        <?php require_once('Connections/conn.php'); ?>
        <?php
        if (!function_exists("GetSQLValueString")) {
        function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
        {
          if (PHP_VERSION < 6) {
            $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
          }

          $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

          switch ($theType) {
            case "text":
              $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
              break;    
            case "long":
            case "int":
              $theValue = ($theValue != "") ? intval($theValue) : "NULL";
              break;
            case "double":
              $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
              break;
            case "date":
              $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
              break;
            case "defined":
              $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
              break;
          }
          return $theValue;
        }
        }
        ?>
        <?php
        // *** Validate request to login to this site.
        if (!isset($_SESSION)) {
          session_start();
        }

        $loginFormAction = $_SERVER['PHP_SELF'];
        if (isset($_GET['accesscheck'])) {
          $_SESSION['PrevUrl'] = $_GET['accesscheck'];
        }

        if (isset($_POST['username'])) {
          $loginUsername=$_POST['username'];
          $password=$_POST['password'];
          $MM_fldUserAuthorization = "";
          $MM_redirectLoginSuccess = "profileForm.php";
          $MM_redirectLoginFailed = "registerForm.php";
          $MM_redirecttoReferrer = false;
          mysql_select_db($database_conn, $conn);

          $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s",
            GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

          $LoginRS = mysql_query($LoginRS__query, $conn) or die(mysql_error());
          $loginFoundUser = mysql_num_rows($LoginRS);
          if ($loginFoundUser) {
             $loginStrGroup = "";

            if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
            //declare two session variables and assign them
            $_SESSION['MM_Username'] = $loginUsername;
            $_SESSION['MM_UserGroup'] = $loginStrGroup;       

            if (isset($_SESSION['PrevUrl']) && false) {
              $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];  
            }
            header("Location: " . $MM_redirectLoginSuccess );
          }
          else {
            header("Location: ". $MM_redirectLoginFailed );
          }
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        <!--link CSS-->
        <link href="style/stylesheet.css" rel="stylesheet" type="text/css" />

        <body>
        <!--<?php require_once('header.php'); ?>-->
              <div  id="loginForm"><table width="250" border="0" align="right">
          <form id="loginForm" name="loginForm" method="POST" action="<?php echo 

$loginFormAction; ?>">
            <tr>
              <td><label for="username">User Name</label></td>
              <td><input type="text" name="username" id="username" /></td>
            </tr>
            <tr>
              <td><label for="password">Password</label></td>
              <td><input type="text" name="password" id="password" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td bgcolor="#FFFFFF"><input type="submit" name="login" id="login" value="Log In" />
                <a href="registerForm.php"><strong> Register</strong></a></td>
            </tr>
          </form>
        </table> 
        </div>
        <?php require_once('footer.php'); ?>
        </body>
        </html>

profileForm.php

个人资料页

<body>

<?php require_once('header.php'); ?>


    <?php
    $con = mysql_connect("localhost","root","root");
    if(!$con)
    {
    die('Could not connect: '.mysql_error());
    }
    mysql_select_db("testregister",$con);

    $username =(isset($_POST['username']));

    $result =mysql_query("Select * from users");

    while ($row=mysql_fetch_array($result))
    {
    echo "<table border='0'>
    <tr>
    <td> First Name: </td>
    <td>" .$row['firstname'] ." </td>
    </tr>
    <tr>
    <td> Last Name: </td>
    <td>" .$row['lastname'] ." </td>
    </tr>

    <tr>
    <td> Birth Date: </td>
    <td>".$row['birthdate'] ." </td>
    </tr>

    <tr>
    <td> Phone Number: </td>
    <td> ".$row['phonenumber'] ." </td>
    </tr>

    <tr>
    <td> Gender: </td>
    <td> ".$row['gender'] ." </td>
    </tr>

    <tr>
    <td> Country: </td>
    <td>".$row['country'] ."</td>
    </tr>

    <tr>
    <td> Specialization: </td>
    <td>".$row['specialisation'] ."</td>
    </tr>

    <tr>
    <td> Email: </td>
    <td>".$row['email'] ."</td>
    </tr>


    <tr>
    <td> User Name: </td>
    <td>".$row['username'] ."</td>
    </tr>

    <tr>
    <td> Join Date: </td>
    <td>".$row['joindate'] ."</td>
    </tr>";
    }
    echo "</table>";


    mysql_close ($con);
    ?>
    <html>
    <body>
    Login Successful
    <p><a href="logout.php\">Click here to logout!</a></p>
    </body>
    </html>





</body>
</html>
4

4 回答 4

0

您已用于$_POST['username']获取用户名。它行不通。

您不能跨两个页面传递 $_POST 变量。您必须使用会话并将登录的用户名传递到会话变量中,然后在您的个人资料页面中调用它。

有关会话的更多信息,请参阅

也使用 $result =mysql_query("Select * from users WHERE username='$username'");代替$result =mysql_query("Select * from users");

于 2013-02-16T20:29:50.480 回答
0

registerprocess.php

session_start() 
$_SESSION['username'] = $username;

$sql = "INSERT INTO users ".

并在中loginForm.php,将您的 sql 查询更改为我的

$user = $_SESSION['username'];
$result =mysql_query("Select * from users where username=".$user."");

类似的东西,这是逻辑,而不是确切的代码...... HTH

于 2013-02-16T20:33:46.547 回答
0

如果您的意思是,如何仅选择从数据库登录的用户,请执行以下操作:

在登录文件loginForm上,创建一个保存用户标识或用户名的会话

$_SESSION['username'] = $loginUsername;

我可以看到您已经创建了一个名为“MM_Username”的会话变量,因此您可以使用它。我建议使用行的 id 而不是用户名。

然后,profileForm只需选择与用户名与查询匹配的行:

$the_query = mysql_query(sprintf("SELECT * FROM users WHERE username='%s'",$_SESSION["MM_Username"]));

就是这样,记住数据库调用中的安全性!但这是另一个话题。祝你好运!

(记得先打电话session_start()!)

session_start()“激活”会话的使用并获取会话信息。所以在开始使用任何 $_SESSION 之前调用它很重要

$_SESSION是一个可以在任何 php 文件中找到的全局变量,$_SESSION["MM_Username"]存在于所有 php 文件中,因此您可以在profileFormfor 选择该特定用户名时使用它。

于 2013-02-16T20:39:57.987 回答
0

我建议使用 mysqli 而不是 mysql。mysql 已被弃用并且不会持续很长时间,而 mysqli 是 mysql 的现代版本。更多信息:这里

于 2019-02-20T20:27:20.180 回答