Jump to content

class global


chriscloyd

Recommended Posts

i have two classes and I am trying to call one class in through another here is my code but my welcome is not showing at all ever when i change the var's to 1

class site {
     function DisplayContent ($page) {
        global $Users;
        echo '<div align="center">
                <div id="Header">
                <div id="NavBar">';
                if ($Users->logged == 1) {
                    echo 'Welcome '. $Users->user .' - <a href="?page=viewreminders">View Reminders</a> - <a href="?page=addreminder">Add Reminder</a> - <a href="logout.php" id="Logout">Logout</a>';
                } else {
                    echo '<a href="?page=login">Login</a> - <a href="?page=register">Register</a> - <a href="?page=forgotpassword">Forgot Password?</a>';
                }
        echo '</div>
            </div>
            <div id="Content">';
            $this->ShowPage($page);
            echo '</div>
            <div id="Footer">Copyright © '.date('Y').'<br />
            Coded and Designed by Chris Cloyd</div>
        </div>';
     }
}

class users {
    
    var $logged;
    var $user;
    var $email;
    var $level;
    
    function LogIn ($name,$email,$level) {
        $this->logged =  1;
        $this->user = $name;
        $this->email = $email;
        $this->level = $level;
    }
    
    function LogOut () {
        $this->logged =  "0";
        $this->user = "";
        $this->email = "";
        $this->level = "";
    }
    
}

Link to comment
Share on other sites

Could it be that when it goes to the login page its starting a new one here my index.php

<?php

/**
* @author Chris Cloyd
* @copyright 2011
*/
/** Session Start For Users */
session_start();
/** Load Config */
include("load/config.php");
/** Load Class.Site and Class.Reminder */
include("load/class.site.php");
include("load/class.reminder.php");
include("load/class.users.php");
/** Start Classes */
$Site = new site();
$Reminder = new reminder();
$Users = new users();
/** Start Html by loading $Site->DisplayHeader(); */
$Site->DisplayHeader();
echo $Users->user;
/** Load Middle Content by loading $Site->DisplayContent(); */
if (isset($_GET['page'])) {
    $Site->DisplayContent($_GET['page']);
} else {
    $Site->DisplayContent('login');
}
/** End Html by loading $Site->DisplayFooter(); */
$Site->DisplayFooter();
?>

 

login.php

<?php

/**
* @author Chris Cloyd
* @copyright 2011
*/
/** Session Start For Users */
session_start();
/** Load Config */
include("../load/config.php");
/** Load Class.Site and Class.Reminder */
include("../load/class.site.php");
include("../load/class.reminder.php");
include("../load/class.users.php");
/** Start Classes */
$Site = new site();
$Reminder = new reminder();
$Users = new users();

$email = mysql_escape_string($_POST['emailLogin']);
$password = mysql_escape_string($_POST['passwordLogin']);
$password = md5($password);

$Search = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND password = '".$password."' ");
$Found = mysql_num_rows($Search);
if ($Found > 0) {
    $UserInfo = mysql_fetch_assoc($Search);
    if ($UserInfo['status'] != 1) {
        $_SESSION['umadBroEmail'] = $email;
        header("Location: http://cloydprojects.com/index.php?page=activate");
    }
    $Users->LogIn($UserInfo['name'],$UserInfo['email'],$UserInfo['level']);
    echo $Users->user;
    $_SESSION['umadBro'] = $UserInfo['name'];
    //header("Location: http://cloydprojects.com/index.php?page=home");
} else {
    $_SESSION['umadBroError'] = 'Incorrect User Information, Try Again!';
    //header("Location: http://cloydprojects.com/index.php?page=login");
}

?>

Link to comment
Share on other sites

Do not use globals with classes. It completely breaks the encapsulation that they provide, you may as well not use classes at all.

 

Search for some tutorials on "PHP Dependency Injection" to get some ideas on how it should be done. A very simple form of dependency injection is to simply have one object accept the other object through an argument in its __construct.

 

eg;

 

class a {}

class b {
  private $a;
  public function __construct(a $a) {
    $this->a = $a;
  }
}

$b = new b(new a);

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.