Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

Why i cant log in ?

HI, i want to log in the user in, but it doesnt work.

index.php

<?php
require 'core.inc.php';
require 'connect.inc.php';


if (isset($_SESSION['user_id']) &&!empty($_SESSION['user_id'])) {
  echo 'Youre logged in';
} else {
  include 'loginform.inc.php';
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Membership Bukkie</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <!-- Seo meta -->
    <meta name="keywords" content="" />

  <!-- Custom CSS -->
        <link href="includes/css/styles.css" rel="stylesheet">
        <link rel="stylesheet" href="fonts/font-awesome.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

   </head>
    <body>






    <script src="jquery-2.1.0.min.js"></script>
    <script src="app.js"></script>

    </body>
</html>

loginform.inc.php

<?php

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

    $password_hash = md5($password);

    if(!empty($username) &&!empty($password)) {

        $query = "SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password_hash'";
        if ($query_run = mysqli_query($con, $query)) {
            $query_num_rows = mysqli_num_rows($query_run);

            if ($query_num_rows==0) {
                echo 'Invalid username/password combination.';
            } else if ($query_num_rows==1) {
                $user_id = mysqli_num_rows($query_run, 0, 'id'); // After this line it doenst work
                 $_SESSION['user_id']=$user_id;
                 header('Location: index.php');
            }
        }


    } else{
        echo 'You must supply a username and password.';
    }
}

?>

```html
<form action="<?php echo $current_file; ?>" method="POST">
    Username: <input type="text" name="username">
    Password: <input type="password" name="password">
    <input type="submit" value="Log in">
</form>
core.inc.php

```php

<?php
ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];

?>

connect.inc.php

<?php

$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_pass = 'root';

$mysqli_db = 'a_database';

error_reporting(E_ALL);  // reports all errors to make it easier to debug

$con = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass);

if (!$con) {
    die("Could not connect to the database server");
} elseif (!mysqli_select_db($con, $mysqli_db)) {
    echo "Could not select the database<br />";
    die(mysqli_error($con));
}

?>

And that all : p The code works, but it doesnt when i log in, it doesnt take the user info from the database.

2 Answers

Jayden Spring
Jayden Spring
8,625 Points

In the login form.inc.php before you do the header redirect you are trying to set $_SESSIOM not SESSION - this will generate a PHP warning.

I suggest you check your logs in future which 90% of the time will help you solve a problem - or put in your code ERROR_REPORTING(E_ALL) when debugging

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you. But it still doesn't work : /

Jayden Spring
Jayden Spring
8,625 Points

Turn on error reporting and please post the error here.

Konrad Pilch
Konrad Pilch
2,435 Points

I did echo.out the line you wrote but no.errors are appearing.

Jayden Spring
Jayden Spring
8,625 Points

Ok it's likely your PHP is set to always output to the log file. If your on Linux go to /var/log/httpd/error_log