0

I'm currently coding a CMS in PHP in order to get back into PHP (I use to use it all the time). However, for some odd reason, when "including" or "requiring" my classes file, it simply stops the php script, my login form (login.php's html) does not show up (whether I am logged in or not). Any help? Here are two of my scripts:

login.php:

<?php
session_start();
include "classes.php";
if(isset($_GET['logout'])) {
    setupSession(2); 
}
if($_SESSION['status'] == "online") header("location: admin.php");
if($_POST && isset($_POST['username']) && isset($_POST['password'])) {
    $un = $_POST['username'];
    $pwd = $_POST['password'];

    $mysql = new mySql();
    $mysql->validateUser($un, $pwd);
} else $attempt = 2;

?>  
<html>
<head>
    <title>Log In</title>
</head>
<body>
<form method="post" action="">
    <label for="username">username: </label>
    <input type="text" name="username" />

    <label for="password">password: </label>
    <input type="password" name="password" />

    <input type="submit" value="Log In" name="submit" />
</form>
</body>
</html>

and classes.php

<?php

class mySql {

    protected $dbname;
    protected $dbuser;
    protected $dbpass;
    protected $db;
    private $conn;

    function __construct() {
        $conn = new mysqli($dbname, $dbuser, $dbpass, $db);
    }

    public function validateUser($username, $password) {
        $query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1";

        if($stmt = $this->conn->prepare($query)) {
            $stmt->bind_param('ss', $username, $password);
            $stmt->execute();

            if($stmt->fetch()) {
                $stmt->close();
                setupSession(1);
            } else $attempt = 1;
        }
    }
}

function setupSession($status) {
    switch($status) {
        case 1:
            $_SESSION['status'] = "online";
            //other user variables
            header("location: admin.php");
            break;
        case 2:
            unset($_SESSION['status']);
            if(isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time() - 1000);
            }
            session_destroy();
            break;
        default:
            session_start();
            if($_SESSION['status'] != "online") header("location: login.php");
            break;
    }
}

?>  
3
  • Is your error_reporting turned on`? Commented Jun 29, 2010 at 17:04
  • 3
    Also note that you must, must, must die() after a header("location:...") otherwise the protected content will be sent to the client in the document's body. Commented Jun 29, 2010 at 17:05
  • Not sure about error_reporting, I'm using MAMP for development. And for the die(), thanks. Commented Jun 29, 2010 at 22:45

4 Answers 4

3

You have a scope problem.

$conn = mysqli(....)

should be $this->conn = mysqli(....)

Sign up to request clarification or add additional context in comments.

3 Comments

Right ! I have bad Java habits :)
Thanks. Fixed, still won't run.
Fixed, WILL WORK. I pasted all my code back in, applied this, and it worked. There were some other errors, but my main problem was this.
2

There are not lots of reasons for a required script to break the parent : the required file does not exist, it has an error or it calls exit() or die().

Are you sure that the file classes.php is in the same folder as your script, or in the include path ?


Is this the exact code you are using ?

With a constructor like this :

function __construct() {
    $conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}

How the hell do you connect to your database ?

$mysql = new mySql();

2 Comments

new mySql(); refers to my class mysql, when with the new instance, the construct does its magic.
and classes.php is in the same folder.
1
function __construct() {
    $conn = new mysqli($dbname, $dbuser, $dbpass, $db);
}

Should Be

function __construct($dbname, $dbuser, $dbpass, $db) {
    $this->dbname = $dbname;
    $this->dbuser = $dbuser;
    $this->dbpass = $dbpass;
    $this->db     = $db;
    $this->connect();
}

function connect()
{
    $this->conn = new mysqli($this->dbname, $this->dbuser, $this->dbpass, $this->db);
}

Something of that nature.

1 Comment

It worked for me when I combined both methods. (Yours did too, but i didn't see any reason to have more code.) function __construct() { $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); }
0
error_reporting (1);

1 Comment

MAMP has error reporting on, but it does not have display errors off. I believe line 277 in the php.ini (for MAMP.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.