Major PHP/MySQL Login Project, with session variab

2019-03-06 10:20发布

问题:

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.

Login Form (just the snippet):

<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>

Check Login

<?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";
            }
        ?>

Members Page:

<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>

回答1:

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

echo $_SESSION['user']

should be

echo $_SESSION['myusername']


回答2:

In your members page change your session name to match your login procesing page. You have "myuserame" and then "user".