主要的PHP/MySQL登录项目,带有会话变量 [英] Major PHP/MySQL Login Project, with session variables

查看:95
本文介绍了主要的PHP/MySQL登录项目,带有会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在过去的两天里,我一直在搜寻互联网,并尽我最大的努力来构建一个相当复杂的登录系统.我已经实现了它的核心,用户可以注册并登录.我对简单的php和mysql并不陌生,但是当涉及到像这样的深入代码时,我迷路了.基本上,我想要的是让用户输入他们的登录名,然后(显然)进行验证,然后完成操作,然后重定向到一个成员页面,该页面仅显示与他们的用户名/帐户有关的信息.我已经在checklogin.php(用于验证登录的文件,请在下面查看)上注册了一些会话变量,但是由于我的一生,我无法将变量传递到实际的成员页面.所以我想我会轻松开始.我只想尝试转移用户名并显示一条欢迎消息您好,[用户名曾经在这里登录].我走不了那么远.有人可以帮帮我吗?一旦得到,我就可以从那里去.

Alright, so for the past two days, I've been scouring the internet and trying my best to put together a rather sophisticated login system. I've got the core of it working, users can sign up, and login. I'm not new to simple php and mysql, but when it comes to in-depth code like this I'm lost. Basically what I want is to have the users enter their login, then have it verified (obviously) and I have that done, then be redirected to a members page that displays information pertaining to their username/account only. I've registered some session variables on the checklogin.php (the file where I verify the log in's, please look below), but for the life of me I cannot get the variables passed to the actual members page. So I figured I'd start easy. I'd just try and transfer the username and display a welcome message "Hello there, [username used to login here]. I cannot get that far. Can someone help me out? Once I get this, I can go from there.

登录表单(仅是代码段):

<form class="form-signin" role="form" method="post" action="checklogin.php">
         <center><img src="logo.png" style="padding-bottom: 10px;"></center>
         <input type="text" name="myusername" id="myusername" class="form-control" placeholder="Email address"  required autofocus><br>
         <input type="password" name="mypassword" id="mypassword" class="form-control" placeholder="Password"  required>
         <br>
         <button class="btn btn-lg btn-primary btn-block" name="Submit" type="submit">Sign In</button>                  
         </form>

检查登录名

<?php
            session_start();
            $host="localhost"; // Host name 
            $username="username"; // Mysql username 
            $password="********"; // Mysql password 
            $db_name="joinfbla_services"; // Database name 
            $tbl_name="members"; // Table name

            // Connect to server and select databse.
            mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
            mysql_select_db("$db_name")or die("cannot select DB");

            // Define $myusername and $mypassword 
            $myusername=$_POST['myusername']; 
            $mypassword=$_POST['mypassword']; 

            // To protect MySQL injection (more detail about MySQL injection)
            $myusername = stripslashes($myusername);
            $mypassword = stripslashes($mypassword);
            $myusername = mysql_real_escape_string($myusername);
            $mypassword = mysql_real_escape_string($mypassword);
            $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
            $result=mysql_query($sql);

            // Mysql_num_row is counting table row
            $count=mysql_num_rows($result);

            // If result matched $myusername and $mypassword, table row must be 1 row
            if($count==1){

            // Register $myusername, $mypassword and redirect to file "login_success.php"
            $_SESSION['myusername'] = $_POST['myusername'];
            $_SESSION['mypassword'] = $_POST['mypassword'];
            echo '<META http-equiv="refresh" content="1;URL=http://joinfbla.com/2014/members/services.php">';
            }

            else {
            echo "Wrong Username or Password";
            }
        ?>

会员页面:

<div class="col-md-4">
            <div class="panel panel-primary">
              <div class="panel-heading">Hello, <?php session_start(); echo $_SESSION['user']?>!</div>
              <div class="panel-body">
                <u><b>BAA Username:</b></u> [Insert PHP/MySQL Here]
                <br>
                <u><b>BAA Password:</b></u> [Insert PHP/MySQL Here]
                <br>
                <u><b>Point's Recorded:</b></u> [Insert PHP/MySQL Here]
                <br>
                <div class="alert alert-warning">Note: Only seniors get points this year, if that field is blank do not fear.</div>
              </div>
            </div>
            <div class="panel panel-primary">
              <div class="panel-heading">This Month in FBLA!</div>
              <div class="panel-body">
                [insert gcalendar here]
              </div>
            </div>
          </div>

推荐答案

我刚刚注意到一件事,但可能无法完全解决问题:

Just one thing I noticed right away, but might not completely solve the problem:

echo $_SESSION['user']

应该是

echo $_SESSION['myusername']

这篇关于主要的PHP/MySQL登录项目,带有会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆