duanbi2760 2017-01-25 03:12
浏览 72
已采纳

登录错误并登录优化[重复]

This question already has an answer here:

Hey guys I have been working on a social networking site and have been working on the login and registration systems. I am pretty new to php so my code may not be the best. The issue I am having right now is my login. This is my log in form on my index.php page.

<form name="loginform" method="post" action="includes/login.php" >
  <table>

  <input type="submit" id="submitbox" name="login" value="Submit">

  <input type="password" name="password" id="passwordbox" required="true" placeholder="Password">

  <input type="text" name="username" id="usernamebox" required="true" placeholder="Username">

  </table>

</form>

I send the form to my login.php page (includes folder) and process the information and if successful start the session and redirect the user to the home.php page. When I click submit to log me in I am brought to an error saying:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /Applications/XAMPP/xamppfiles/htdocs/VolleypagesFixed/includes/login.php on line 14

Warning: mysqli_error() expects parameter 1 to be mysqli, resource given in /Applications/XAMPP/xamppfiles/htdocs/VolleypagesFixed/includes/login.php on line 14

This line is on my log in page and this is the code for that page:

<?php
// start the session
session_start();

require_once ('connect.php');

// if form is submitted

if(isset($_POST['username']) and isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];

$query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";

line 14 - $result = mysqli_query($con, $query) or die(mysqli_error($con));
$count = mysqli_num_rows($result);

// create session if values are equal
if($count == 1) {
    // cookie is set
   if(isset($_COOKIE['ID'])) {
            $id = mysql_real_escape_string($_COOKIE['ID']);
        }
        if(!isset($id)){
            die("Cookie not found");
        }
} else {
    echo "Invalid Login";
    header("Location: http://localhost/FolderName/index.php");
}
}

// if user is logged in redirect
if(isset($_COOKIE['ID'])) {
header("Location: http://localhost/FolderName/home.php");
}

?>

Any help on the error on line 14 or any advice and how I have set my login up would be very helpful!

</div>
  • 写回答

2条回答 默认 最新

  • douxin1884 2017-01-25 03:24
    关注

    Try this

    Use

     mysqli_connect('localhost', 'root', 'root', 'test');
    

    Instead:

      mysql_connect
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongyan6910 2017-01-25 03:18
    关注

    Quick solution:

    Add the "i" in mysqli_connect.

    Explanation:

    This error indicates that you don't have a valid connection in your variable $con. connect.php is where you establish the connection resource to your MySQL server. However, your code shows that you're using the older, deprecated mysql_connect function in your connection, and then trying to use the resource that it returns with the newer mysqli_query function. You can't combine these two functions. You should ensure that you are correctly using the mysqli_connect() function and saving the result to $con before trying to perform a query. Note the missing i in your initial connection. mysql_* functions are deprecated (due to security concerns). Use the newer mysqli_* set of functions.

    For more information on usage:

    http://php.net/manual/en/function.mysqli-connect.php

    Usage Example:

    $con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    

    It's not necessary to have a separate mysql_select_db since you can select the database in your connection string.

    Help with Your Other Issues

    Although beyond the scope of your original question, you're going to need to make quite a few improvements to how you're getting the data and storing it with cookies. I'll make a few suggestions:

    <?php
    // start the session
    session_start();
    
    require_once ('connect.php');
    
    // if form is submitted
    
    if(isset($_POST['username']) and isset($_POST['password'])) {
      $username = $_POST['username'];
      $password = $_POST['password'];
    
      $query = "SELECT * FROM `users` WHERE username='$username' and password='$password'";
    
      $result = mysqli_query($con, $query) or die(mysqli_error($con));
      $count = mysqli_num_rows($result);
    
      // create session if values are equal
      if($count == 1) {
        $row = mysqli_fetch_assoc($result); // This gets the first (and only) result of your query.
        // save id as cookie
        $_COOKIE['ID'] = $row['id']; // Or whatever ID you have in your users table
      } else {
        header("Location: http://localhost/FolderName/index.php");
        exit;
      }
    }
    // if user is logged in redirect
    if(isset($_COOKIE['ID'])) {
      header("Location: http://localhost/FolderName/home.php");
      exit;
    }
    

    This code is FAR from perfect or complete, but much closer than what you currently have. Hopefully you can learn from the lines of code that I've changed for you (like fetching a row and setting a cookie).

    You were also echoing and using a header which would fail, since that is considered as output before header.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求日版华为b610s-77a 官方公版固件,有偿
  • ¥15 关于#java#的问题,请各位专家解答!(相关搜索:java程序)
  • ¥15 linux tsi721的驱动编译后 insmod 提示 报错
  • ¥20 multisim测数据
  • ¥15 求无向连通网的所有不同构的最小生成树
  • ¥15 模拟器的framebuffer问题
  • ¥15 opencv检测轮廓问题
  • ¥15 单点式登录SSO怎么爬虫获取动态SSO_AUTH_ACCESS_Token
  • ¥30 哈夫曼编码译码器打印树形项目
  • ¥20 求完整顺利登陆QQ邮箱的python代码