Jump to content

Recommended Posts

Basically i have a class which is declared within the index page but say i go to a new page and i use a meta refresh the class wont be accessiable for some reason, i try to print/echo the information but nothing is displayed. The infomration is getting stored in the class because if i print/echo from the index it will display the information. I was thinking maybe i would have to store the class in a session but not 100% sure how i would go about retrieving the information, So does anyone have any idea?

Link to comment
https://forums.phpfreaks.com/topic/87104-how-can-i-use-class-from-all-over-site/
Share on other sites

Class:

 

<?php

require("./include/MySql/MySqlConnect.php");

class UserHandler {

var $loggedIn = false;

var $usersID = "";
var $usersInfo = array();
var $usersAccountInfo = array();

var $mysqlConnection = false;
var $mysqlConnected = false;

function UserHandler(){
	if($this->mysqlConnection =connect())
	{
		$this->mysqlConnected = true;
		//check weather user has sessions already
		return true;
	}else{
		echo 'Error Connecting To Database';
		return false;
	}

}

function Login ($email = "",$password = ""){
	if($this->mysqlConnected)
	{
		//First check the user exists and check the details they enetered with database
		$this->mysqlConnection->query("SELECT * FROM sns_accounts WHERE account_email='". $email . "' AND account_md5pass='" . md5($password) . "' ");

		if($this->mysqlConnection->numRows() > 0){
			$this->usersInfo = $this->mysqlConnection->fetchArray();
			$this->usersId = $this->usersInfo[0];
		}

		//Second pull all their account info so we can display on their profile page
		$this->mysqlConnection->query("SELECT * FROM sns_account_info WHERE account_info_account_id='". $this->usersInfo[0] . "' ");

		if($this->mysqlConnection->numRows() > 0){
			$this->usersAccountInfo = $this->mysqlConnection->fetchArray();
			$this->loggedIn = true;
		}

		if($this->loggedIn)
		{
			return true;
		}
	}
}

function Register($email = "",$password = "",$profileDir = "",$firstName = "",$lastName = "") {
	if($this->mysqlConnected)
	{
		$this->mysqlConnection->query("SELECT * FROM sns_accounts WHERE account_email='". $email ."' ");

		if($this->mysqlConnection->numRows() <= 0){
			//E-Mail not used lest continue
			//build first query
			//md5 our pass
			$md5pass = md5($password);
			$this->mysqlConnection->query("INSERT INTO sns_accounts (`account_id`,`account_email`,`account_pass`,`account_md5pass`,`account_created`) 
				VALUES ('','$email','$password','$md5pass',NOW())");

			//Get the id used with the new account
			$this->mysqlConnection->query("SELECT * FROM sns_accounts WHERE account_email='". $email ."' ");
			if($this->mysqlConnection->numRows() > 0){
				//store info in varaiable 
				$this->usersInfo = $this->mysqlConnection->fetchArray();
				$this->usersId = $this->usersInfo[0];

			}else{
				echo 'Error Whilst Creating Account, Please Contact Site Admin.';
				return false;
				//was error creating account somewhere
			}

			//so now we have id we can complete the registration
			$this->mysqlConnection->query("INSERT INTO sns_account_info (`account_info_id`,`account_info_account_id`,`account_info_first_name`,`account_info_last_name`,
			`account_info_about`,`account_info_friends`,`account_info_profile_directory`) 
						VALUES ('','$this->usersId','$firstName','$lastName','','','$profileDir')");

			//all info should now be enetered into the system
			echo 'Account Create Succesfully, You Can Now Login.';

			return true;
		}else {
			echo "E-Mail Already In Use, Please Try Another Or Recovery Password.";

			return false;
		}
	}

}

function UpdateAccount(){

}

}

?>	

 

index:

 

<?php 
session_start();
require("./include/PageSwitch.php");
require("./classes/UserHandler.php");

if(isset($_SESSION['userHandler'])){
$userHandler = $_SESSION['userHandler'];
echo'1';
}else {
$userHandler = new UserHandler();
$_SESSION['userHandler'] = $userHandler;
echo'2';
}


//Login Submit to execute handler function
if(isset($_POST["submit"])){
if($userHandler->Login($_POST['email'],$_POST['password']) == true)
{
	//redirect to there profile
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php?p=profile&m_id=" . $userHandler->usersInfo[0] . "\">";
	//echo 'Logged in, Welcome ' . $userHandler->usersInfo[0];
}else {
	echo 'Error';
}
}

//Register Submit to execute handler function
if(isset($_POST["reg"])){

if($userHandler->Register($_POST['email'],$_POST['password'],$_POST['profileDir'],$_POST['firstName'],$_POST['lastName'])){

}else {
	echo 'Error';
}
  
}

//EditProfile Submit to execute handler function
if(isset($_POST["editP"])){
if($userHandler->UpdateAccount()){

}else{
	echo 'Error';
}
}

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="include/stylesheet.css">
<title>Social Network Site <?php echo $userHandler->mysqlConnected; ?></title>
</head>

<body>
<table width="700" height="277" border="0" class="MainTable">
  <tr>
    <td id="Header" height="105" background="Header.gif"> </td>
  </tr>
  <tr>
    <td id="Navigation" height="21" ><?php include("./include/NavigationTable.php"); ?></td>
  </tr>
  <tr>
    <td id="Content" height="124"><?php include("./include/". ChangePage($_GET['p'] ).".php"); ?></td>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>
</body>
</html>

Web servers and web pages are stateless. When the code on any page reaches the end of that page, all resources used in that code are destroyed.

 

To make any data carry between pages, you would need to store it in a session. You can either serialize your object data and save it in a session variable or you can directly create a class in a session and as long as the class definition is present when the session_start() is executed, the class data will be available on another page.

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.