php 如何点击按钮,并转到个人资料页,如果我登录帐户

nkhmeac6  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(52)

我做了一个主页,其中有一个按钮,供用户登录或去配置文件。
我希望这个按钮有这样的功能:当登录时,按钮将显示用户名,如果点击将使用户的个人资料页面。当不登录,按钮将显示登录/注册,如果点击将使用户登录页面。
主页按钮:

<a href="login.php" class="get-started-btn">
  <?php if (mysqli_num_rows($result) == 1 ){
                //log user in
                $_SESSION["user_id"] = $id['user_id'];
                echo $username;
                //here put profile.php
            }else{
                echo "登入/注册"; //login/register
                //here put login.php 
            } ?>
</a>

字符串
登录/注册_服务器php:

$username = '';
$email = '';
$password = '';
$errors = array();
        
//connect to the database
$db = mysqli_connect('localhost', 'root', '', 'hdhouse');

//If register button is clicked
if (isset($_POST['register'])) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

        $user_check_query = "SELECT * FROM user WHERE email='$email' LIMIT 1";
        $check = mysqli_query($db, $user_check_query);
        $user = mysqli_fetch_assoc($check);

        if ($user) { // if user exists

          if ($user['email'] === $email) {
            array_push($errors, "email already exists");
          }
        }
    
   //To make sure that form are filled
    if(empty($username)){
        array_push($errors, "Username is Required!");
    }
    if(empty($email)){
        array_push($errors, "Email is Required!");
    }
    if(empty($password)){
        array_push($errors, "Password is Required!");
    }else{
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            array_push($errors, "Invalid email format");

    }
    if(!isset($_POST['term_check'])){
        array_push($errors, "Please check the terms & conditions!");
      } else {
        // nothing
      }//If there are no error, save the user info to database
    if(count($errors) == 0){
        //encrypt password before stroing into database
        $password = ($password);
        
        $sql = "INSERT INTO user (username, email, password) 
        VALUES ('$username', '$email', '$password')";
        mysqli_query($db, $sql);
        $_SESSION['success'] = "Register Success!";
        header('location: login.php');
    }
}
//log user in from login page
if(isset($_POST['login'])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    
   //To make sure that form are filled
    if(empty($username)){
        array_push($errors, "Userame is Required!");
    }
    if(empty($password)){
        array_push($errors, "Password is Required!");
    }
    
    if(count($errors) == 0){
        $password =($password); //compare the password form database
        $query = "SELECT * FROM user WHERE username ='$username' and password ='$password'";
        $result = mysqli_query($db, $query);
        $id = mysqli_fetch_array($result);
        if (mysqli_num_rows($result) == 1 ){
            //log user in
            $_SESSION["user_id"] = $id['user_id'];
            header('location: 主页.php'); //home.php

        }else{
            array_push($errors, "Wrong Username or Password!");
        }
    }
}

2wnc66cl

2wnc66cl1#

呈现按钮时,可以检查相关的Session值,以查看用户是否已登录。
例如,类似于以下内容:

<?php 
if (isset($_SESSION["user_id")) {
  echo '<a href="login.php" class="get-started-btn">Log in</a>';
}
else
{
  echo '<a href="profile.php" class="get-started-btn">View Your Profile</a>';
}

字符串

相关问题