如何正确编写登录后显示用户名的PHP代码?

时间:2017-06-23 13:11:50

标签: php html

登录后我需要帮助编写显示用户名的代码!我一直在关注一些视频来帮助我整个代码,所以我为任何滑倒道歉。我是初学者。

Profile.php

       <?php 
           require("connect.php");
           require("functions.php");
           if(logged_in())
           {

HTML

    <a href="changepassword.php">Change Password</a>
    <a href="logout.php" style="float:right; padding:10px; margin-right:40px; background-color:#eee; color:#333; text-decoration:none;">Logout</a>
    <!--This is where I'm going to add my php code to display-->
    <p> {first name} {last name} Profile</p>
    <p> {username}</p>


<?php 

    }
    else
    {
    header("location:login.php");
    exit();
    }

?>

的functions.php

<?php 
    function email_exists($email, $con)
    {
    $result = mysqli_query($con,"SELECT id FROM users WHERE email='$email'");

    if(mysqli_num_rows($result) == 1)
    {
        return true;
    }
    else
    {
        return false;
    }

}

function logged_in()

{   

        if(isset($_SESSION['email']) || isset($_COOKIE['email']))
        {
            return true;
        }
        else
        {
            return false;
        }
}

现在我与数据库的连接

connect.php

<?php

$con = mysqli_connect("localhost","root","****","database");

if(mysqli_connect_errno())
{
    echo "Error occured while connecting with database ".mysqli_connect_errno();
}
session_start();

signup.php

include("connect.php");
include("functions.php");

if(logged_in())
{
    header("location:profile.php");
    exit();
}

$error = "";

if(isset($_POST['submit']))
{
    $firstName = mysqli_real_escape_string($con, $_POST['fname']);
    $lastName = mysqli_real_escape_string($con, $_POST['lname']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $userid = mysqli_real_escape_string($con, $_POST['userid']);
    $password = $_POST['password'];
    $passwordConfirm = $_POST['passwordConfirm'];

    $image = $_FILES['image']['name'];
    $tmp_image = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];

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

    $date = date("F, d Y");


    if(strlen($firstName) < 3)
    {
        $error = "First name is too short";
    }

    else if(strlen($lastName) < 3)
    {
        $error = "Last name is too short";
    }
    else if(strlen($userid) > 8)
    {
        $error = "You need a longer username";
    }
    else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        $error = "Please enter valid email address";
    }
    else if(email_exists($email, $con))
    {
        $error = "Someone is already registered with this email";
    }
    else if(strlen($password) < 8)
    {
        $error = "Password must be greater than 8 characters";
    }
    else if($password !== $passwordConfirm)
    {
        $error = "Password does not match";
    }
    else if($image == "")
    {
        $error = "Please upload your image";
    }
    else if($imageSize > 1048576)
    {
        $error = "Image size must be less than 1 mb";
    }           
    else if(!$conditions)
    {
        $error = "You must be agree with the terms and conditions";
    }
    else
    {   
            $password = password_hash($password, PASSWORD_DEFAULT);

            $imageExt = explode(".", $image);
            $imageExtension = $imageExt[1];

            if($imageExtension == "PNG" || $imageExtension == "png" || $imageExtension == "JPG" || $imageExtension == "jpg")
            {
                $image = rand(0, 100000).rand(0, 100000).rand(0, 100000).time().".".$imageExtension;

                $insertQuery = "INSERT INTO users(firstName, lastName, userid, email, password, image) VALUES ('$firstName','$lastName','$userid','$email','$password','$image')";
                if(mysqli_query($con, $insertQuery))
                {
                    if(move_uploaded_file($tmp_image,"images/$image"))
                    {
                        $error = "You are successfully registered";
                    }
                    else
                    {
                        $error = "Image is not uploaded";
                    }
                }
            }
            else
            {
                $error = "File must be an image";
            }
    }

}

的login.php

if(logged_in())
{
    header("location:profile.php");
    exit();
}

$error = "";

if(isset($_POST['submit']))
{

    $email = mysqli_real_escape_string($con, $_POST['email']);
    $password = mysqli_real_escape_string($con, $_POST['password']);
    $checkBox = isset($_POST['keep']);

    if(email_exists($email,$con))
    {
        $result = mysqli_query($con, "SELECT password FROM users WHERE email='$email'");
        $retrievepassword = mysqli_fetch_assoc($result);

        if(!password_verify($password, $retrievepassword['password']))
        {
            $error = "Password is incorrect";
        }
        else
        {
            $_SESSION['email'] = $email;

            if($checkBox == "on")
            {
                setcookie("email",$email, time()+3600);
            }

            header("location: profile.php");
        }


    }
    else
    {
        $error = "Email Does not exists";
    }




}

3 个答案:

答案 0 :(得分:1)

您可以将任何信息“保存”到会话中,以便您可以使用new_weights = {} for node in G.nodes(): new_weights[node] = sum([G.node[neighbor]['weight'] for neighbor in G[node]]) # sum weights of all neighbors of current node nx.set_node_attributes(G, 'weight', new_weights) # set new weights plt.figure() nx.draw(G, pos=pos, labels=nx.get_node_attributes(G, 'weight'), node_size=700, node_color='w') # draw new node weights plt.show() 变量在其他网页上检索它。例如,如果在session_start()之后添加$_SESSION,则会保存用户名以供将来使用。使用以下标记在HTML中包含用户名:$_SESSION['userid'] = $_POST['userid'];; ?&GT;

答案 1 :(得分:0)

首先,您需要做的是检查用户是否使用所需的详细信息成功登录,这些详细信息主要包含用户名和密码。 之后,您需要在会话中存储用户的用户名。 e.g

<?php // Set session variables $_SESSION["username"] = $username; //The username the user used to login to your site ?>

您需要使用用户名的任何地方,您都可以回复<?php echo $_SESSION['username']; ?>

答案 2 :(得分:0)

登录成功后,您需要创建会话。喜欢

<?php
session_start();
$_SESSION['FirstName'] = firstName; //The firstname the user typed in
$_SESSION['LastName'] = lastName; //The lastname the user typed in
?>