Jump to content

OOP Problems


Drezard

Recommended Posts

Hello, I'm using two files. One is called class.php and it holds a class with all my functions. Then im including it and using the functions from the class in a file called index.php. Now I can call myclass->connect(); and it not give me any errors but i cant call myclass->login_box(); and it not give me errors. Heres the error when i try and call myclass->login_box();

[QUOTE]

Fatal error: Call to undefined method web::login_box() in C:\Program Files\xampp\htdocs\test\index.php on line 9

[/QUOTE]

Heres the code for index.php:

[CODE]
<?php

include('class.php');

$myclass = new web;

$myclass->connect();

$myclass->login_box();

$myclass->newuser();

?>
[/CODE]

And heres class.php:

[CODE]
<?php

error_reporting(E_ALL);

session_start();

class web {

var $connection = NULL;
var $login = NULL;
var $dbhost = "localhost";
    var $dbuser = "test";
    var $dbpass = "test";
    var $db = "sellyourownhome";

function connect() {

$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ("Unable to connect!");
   
    mysql_select_db($this->db) or die ("Unable to select database!");

}

function close_connect() {

mysql_close($this->connection);

}

function add_user() {

$this->connect();

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];
$pass = $userinformation[1];

$query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";

$priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

if ($priv >= 6) {

if (!isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
Comfirm Password: <input type='password' name='pass2'>
Email: <input type='text' name='email'>
Comfirm Email: <input type='text' name='email2'>
Priviledges: <select name='priv'>
<option value='1'>Guest</option>
<option value='2'>User/Buyer</option>
<option value='3'>Seller</option>
<option value='4'>Business</option>
<option value='5'>Moderator</option>
<option value='6'>Admin</option>
<option value='7'>Maintance</option>
</select>
    <input type='submit' name='submit'>
    </form>";

}

if (isset($_POST['user'])) {

$user = $_POST['user'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$priv = $_POST['priv'];

if ($pass == $pass2) {

if ($email == $email2) {

$query = "INSERT INTO users (user, pass, priv) VALUES ('$user', '$pass', '$priv')";

$result = mysql_query($query) or die ("Error in query: $query.");

$query = "INSERT INTO profiles (user, email) VALUES ('$user', '$email')";

$result = mysql_query($query) or die ("Error in query: $query.");

echo "Acount sucessfully created";

}

else {

echo "doesnt match2";

}

}

else {

echo "doesnt match";

}

}

$this->close_connect();

}

if ($priv <= 5) {

echo "You don't have the priveleges to view this.";

}

}

}

function login_box() {

$this->connect();

if (isset($_SESSION['user'])) {

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];

echo 'Welcome, $user';

}

if (!isset($_SESSION['userinfo']) && !isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
    <input type='submit' name='submit'>
    </form>";

}

if (!isset($_SESSION['userinfo']) && isset($_POST['user'])) {

$user = $_POST['user'];
$pass = $_POST['pass'];

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$count = mysql_num_rows($result);

if ($count == 1) {

$_SESSION['userinfo'] = '$user $pass';

}

if ($count == 0) {

echo "Username or password are incorrect.";

}

}

}

function logout() {

if (!isset($_SESSION['userinfo'])) {

echo "You have to be logged in to view this.";

}

if (isset($_SESSION['userinfo'])) {

session_destroy();

echo "You have sucessfully logged out";

}

}
   


?>
[/CODE]

I dont understand whats wrong?

Thanks, Daniel
Link to comment
https://forums.phpfreaks.com/topic/29873-oop-problems/
Share on other sites

Alright I really don't understand this now. What brackets do i need to fix up (E.g  What Lines)

Im getting a different error that i dont understand. Heres the error:

[QUOTE]
arse error: parse error, unexpected ';', expecting T_FUNCTION in C:\Program Files\xampp\htdocs\test\class.php on line 189
[/QUOTE]

And heres the code:

[CODE]
<?php

error_reporting(E_ALL);

session_start();

class web {

var $connection = NULL;
var $login = NULL;
var $dbhost = "localhost";
    var $dbuser = "drezard";
    var $dbpass = "";
    var $db = "sellyourownhome";

function connect() {

$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ("Unable to connect!");
   
    mysql_select_db($this->db) or die ("Unable to select database!");

}

function close_connect() {

mysql_close($this->connection);

}

function add_user() {

$this->connect();

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];
$pass = $userinformation[1];

$query = "SELECT priv FROM users WHERE user='$user' AND pass='$pass'";

$priv = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

if ($priv >= 6) {

if (!isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
Comfirm Password: <input type='password' name='pass2'>
Email: <input type='text' name='email'>
Comfirm Email: <input type='text' name='email2'>
Priviledges: <select name='priv'>
<option value='1'>Guest</option>
<option value='2'>User/Buyer</option>
<option value='3'>Seller</option>
<option value='4'>Business</option>
<option value='5'>Moderator</option>
<option value='6'>Admin</option>
<option value='7'>Maintance</option>
</select>
    <input type='submit' name='submit'>
    </form>";

}

if (isset($_POST['user'])) {

$user = $_POST['user'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$priv = $_POST['priv'];

if ($pass == $pass2) {

if ($email == $email2) {

$query = "INSERT INTO users (user, pass, priv) VALUES ('$user', '$pass', '$priv')";

$result = mysql_query($query) or die ("Error in query: $query.");

$query = "INSERT INTO profiles (user, email) VALUES ('$user', '$email')";

$result = mysql_query($query) or die ("Error in query: $query.");

echo "Acount sucessfully created";

}

else {

echo "doesnt match2";

}

}

else {

echo "doesnt match";

}

}

$this->close_connect();

}

if ($priv <= 5) {

echo "You don't have the priveleges to view this.";

}

}

function login_box() {

$this->connect();

if (isset($_SESSION['user'])) {

$userinformation = explode(" ", $_SESSION['userinfo']);
$user = $userinformation[0];

echo 'Welcome, $user';

}

if (!isset($_SESSION['userinfo']) && !isset($_POST['user'])) {

echo "<form action='' method='post'>
    Username: <input type='text' name='user'>
    Password: <input type='password' name='pass'>
    <input type='submit' name='submit'>
    </form>";

}

if (!isset($_SESSION['userinfo']) && isset($_POST['user'])) {

$user = $_POST['user'];
$pass = $_POST['pass'];

$query = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$count = mysql_num_rows($result);

if ($count == 1) {

$_SESSION['userinfo'] = '$user $pass';

}

if ($count == 0) {

echo "Username or password are incorrect.";

}

}

function logout() {

if (!isset($_SESSION['userinfo'])) {

echo "You have to be logged in to view this.";

}

if (isset($_SESSION['userinfo'])) {

session_destroy();

echo "You have sucessfully logged out";

}

}

}
   


?>

[/CODE]

Is there anything else I need to fix up?

- Cheers, Daniel
Link to comment
https://forums.phpfreaks.com/topic/29873-oop-problems/#findComment-137281
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.