Jump to content

Recommended Posts

Hey,

I'm trying to create a class. I need help. I don't know how to do two things regarding it.

 

<?php

require_once("class.query.php");

require_once("class.forms.php");

class user {

var $username = "";

var $password = "";

var $email = "";

var $status = 0;

 

function user() {

global $usersinfo;

}

#####################

# Check to see if

# a user is logged in.

function isLoggedin() {

if(isset($_SESSION['username']) && isset($_SESSION['password']))

return 1;

return 0;

}

 

##################

# Build Login Form

function buildLogin() {

$forms = new forms();

$forms->createFormBlank("login");

$forms->createTextBoxAdv("Username: ", "username", 12, "if (this.value == '') { this.value = 'Username'; }", "if (this.value == 'Username') { this.value = ''; }", "", "Username", "0");

echo "<br />\n";

$forms->createTextBoxAdv("Password: ", "password", 12, "if (this.value == '') { this.value = 'Password'; }", "if (this.value == 'Password') { this.value = ''; }", "", "Password", "1");

echo "<br />\n";

$forms->createSubmit("submit", "submit");

$forms->endForm();

}

}

?>

In other languages when you call $user = new user();

that also calls the function user(); That would create the global.

 

How can I create a global variable to hold the users info once they login successfully?

So I need to know can you do something like class user { function user() } and user is called automatically AND how is the best way to register a global?

 

Thank You

Link to comment
https://forums.phpfreaks.com/topic/73723-solved-php-class-help/
Share on other sites

To call a class:

 

$user = new user();

 

To call a variable in that class, to either retrieve or store:

 

//store value
$user->username = "some_username";
//retrieve value
$username = $user->username;

 

To run a function in that class (if it contains a return; to get a value from the function)

 

//simply run a function
$user->user();
//run a function that returns a value
$logged_in = $user->isLoggedin();

 

In other languages when you call $user = new user();

that also calls the function user(); That would create the global.

 

How can I create a global variable to hold the users info once they login successfully?

So I need to know can you do something like class user { function user() } and user is called automatically AND how is the best way to register a global?

 

I need to know what you mean by this? A variable that's stated at the top of the class can be referenced from inside or outside the class (IE: var $username). Do you mean that when you call the class, you want to be able to go to a new page, call $user->username; and retrieve the current username data?

Link to comment
https://forums.phpfreaks.com/topic/73723-solved-php-class-help/#findComment-371991
Share on other sites

If you would like to be able to get a username from the class, and have that same class over different pages, I can easily say that there are two methods.

 

One method, recreate the class each time, with the variables previously stored in sessions, or store the whole class in sessions (to be passed over diff pages)

 

 

Method 1: (sessions to store values)

//page 1
$user = new user();
$_SESSION["username"] = $user->username;

//page 2
$user = new user();
$user->username = $_SESSION["username"];

 

Method 2: serialization (store the object in a session)

//top of page1
$user = new user();
$user->username = "bob";

//bottom of page1
$_SESSION["user_object"] = serialize($user);



//top of page2
$user = unserialize($_SESSION["user_object"]);
echo $user->username;//outputs "bob"

 

I believe each method would work, I'm more familiar with using the first method, rather than the second method (sorry).

Link to comment
https://forums.phpfreaks.com/topic/73723-solved-php-class-help/#findComment-372013
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.