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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.