Page 2 of 2

Posted: Thu Jun 28, 2007 5:35 am
by volka
please try

Code: Select all

if ( !isset($user) ) {
	die('there is no $user');
}
else if( !is_object($user) ) {
	die('gettype(user): '.gettype($user));
}
if ($user->_checkLogin($processed['username'], $processed['password'], $remember)) {

Posted: Thu Jun 28, 2007 7:54 am
by m2babaey
Thanks. Your support is really appreciated. :P
It prompts there is no $user upon login now. (I'm sure I'm entering the login details correctly). maybe we should have an overview again:
I include this code on my protected page:

Code: Select all

$user->membersOnly();
function membersOnly:

Code: Select all

function membersOnly() {
        error_reporting(E_ALL);
        ini_set('display_errors', true);
       
        if (!$_SESSION['logged']) {
                $_SESSION['log_to'] = $_SERVER['REQUEST_URI'];
                session_write_close();
            session_start(); 
                $_SESSION['file'] = "login.php";
                include 'message.php';
                            die();
        }
}
and message.php has:

Code: Select all

<?  include $_SESSION['file']; ?>
and login.php after form elements:

Code: Select all

if ($form->valid()) {
		$processed = $form->getProcessed();
		$remember = $form->getValue('remember');

		if (!isset($_SESSION['login'])) {
			$_SESSION['login'] = 3;
		} else {
			if ($_SESSION['login'] <= 1) {
				die('You cannot log in.');
			}
		}
if ( !isset($user) ) {
        die('there is no $user');
}
else if( !is_object($user) ) {
        die('gettype(user): '.gettype($user));
} 
		if ($user->_checkLogin($processed['username'], $processed['password'], $remember)) {
			if (isset($_SESSION['log_to'])) {
				redirect($_SESSION['log_to']);
			} else {
				redirect('account.php/');
			}
		} else {
			failed($form);
		}

	} else {
		begin_html();
		$form->display();
	}

	function failed(&$form) {
		begin_html();
		echo "<p>You could not be logged in, $_SESSION[login] attempts left.</p>
		<p>Possible reasons for this are:</p>

		<ul>
			<li>Your username and/or password is not correct.
				Check your username and password and then try again.</li>
			<li>You haven't " .
			'<a href="signup.php" title="Sign up for an account, it is free">registered</a> yet</li>
			<li>Your account is temporarily disabled.</li>
			<li>You are trying to login simultaneously from two different computers or
			two browsers on the same computer.</li>
		</ul>';

		$form->display();
	}
?>
thanks

Posted: Thu Jun 28, 2007 8:13 am
by volka
hm, sorry. I still have no clue how your script is supposed to work and what is called when, where and why.

please try

Code: Select all

$db = db_connect();
echo '<div>Debug $user = new User<pre>', __FILE__, '@', __LINE__, ' [', __FUNCTION__, "]\n";
print_r(debug_backtrace());
echo "</div>\n";
$user = new User($db);

Code: Select all

if ( !isset($user) ) {
	echo '<div>!isset($user)<pre>', __FILE__, '@', __LINE__, ' [', __FUNCTION__, "]\n";
	print_r(debug_backtrace()); echo "</pre>\n";
	echo '<pre>included files: ', print_r(get_included_files()); echo "</pre>\n";
	echo "</div>\n";
	die();
}
else if( !is_object($user) ) {
	die('gettype(user): '.gettype($user));
}
and post the output.

Posted: Thu Jun 28, 2007 8:36 am
by m2babaey
Each post you are doing is teaching me new functions and ideas. I consider all your posts as tutorials, not just some code to get my script running.
I was not sure where to place that debug codes. tried placing the first one after include $_SESSION['file']; in message.php it said:

Code: Select all

Debug $user = new User

g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php@71 []
Array
(
    [0] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php
            [line] => 240
            [function] => include
        )

    [1] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
            [line] => 7
            [function] => membersonly
            [class] => user
            [type] => ->
            [args] => Array
                (
                )

        )

)
and the second code said:

Code: Select all

!isset($user)

g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php@72 []
Array
(
    [0] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php
            [line] => 240
            [function] => include
        )

    [1] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
            [line] => 7
            [function] => membersonly
            [class] => user
            [type] => ->
            [args] => Array
                (
                )

        )

)

included files: Array
(
    [0] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
    [1] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\global.php
    [2] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\functions.php
    [3] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\config.php
    [4] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\session_defaults.php
    [5] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php
    [6] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB.php
    [7] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\PEAR.php
    [8] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB\mysql.php
    [9] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB\common.php
    [10] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php
    [11] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\includeright.php
    [12] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\includeleft.php
    [13] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\login.php
    [14] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\html_form.php
    [15] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\HTML\Table.php
    [16] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\HTML\Common.php
)
1

Posted: Thu Jun 28, 2007 9:13 am
by volka
m2babaey wrote:here:

Code: Select all

$db = db_connect();
$user = new User($db);
The first code snippet was supposed to be inserted there.
volka wrote:$db = db_connect();
echo '<div>Debug $user = new User<pre>', __FILE__, '@', __LINE__, ' [', __FUNCTION__, "]\n";
print_r(debug_backtrace());
echo "</div>\n";

$user = new User($db);
And the second code snippet should replace (or extend) the old debug code
if ( !isset($user) ) {
die('there is no $user');
}
else if( !is_object($user) ) {
die('gettype(user): '.gettype($user));
}
volka wrote:if ( !isset($user) ) {
echo '<div>!isset($user)<pre>', __FILE__, '@', __LINE__, ' [', __FUNCTION__, "]\n";
print_r(debug_backtrace()); echo "</pre>\n";
echo '<pre>included files: ', print_r(get_included_files()); echo "</pre>\n";
echo "</div>\n";
die();

}
else if( !is_object($user) ) {
die('gettype(user): '.gettype($user));
}

Posted: Thu Jun 28, 2007 9:42 am
by m2babaey
the first time I load the protected page(account.php), it says:

Code: Select all

Debug $user = new User

g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\global.php@11 []
Array
(
    [0] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
            [line] => 4
            [function] => require_once
        )

)


Warning: session_start(): Cannot send session cookie - headers already sent by (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\global.php:11) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php on line 238

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\global.php:11) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php on line 238
and when I logged in, it repeats the above error, plus:

Code: Select all

!isset($user)

g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\login.php@36 []
Array
(
    [0] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php
            [line] => 70
            [function] => include
        )

    [1] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php
            [line] => 240
            [args] => Array
                (
                    [0] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php
                )

            [function] => include
        )

    [2] => Array
        (
            [file] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
            [line] => 7
            [function] => membersonly
            [class] => user
            [type] => ->
            [args] => Array
                (
                )

        )

)

included files: Array
(
    [0] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\account.php
    [1] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\global.php
    [2] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\functions.php
    [3] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\config.php
    [4] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\session_defaults.php
    [5] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\user.php
    [6] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB.php
    [7] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\PEAR.php
    [8] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB\mysql.php
    [9] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\DB\common.php
    [10] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php
    [11] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\includeright.php
    [12] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\includeleft.php
    [13] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\login.php
    [14] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\html_form.php
    [15] => g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\lib\filter.php
)
1
thanks

Posted: Thu Jun 28, 2007 10:02 am
by volka
simplified you have something like

Code: Select all

<?php
$globalvar = 'xyz';

function foo() {
  // echo $globalvar; <- notice undefined variable
  include 'bar.php';
}
You cannot access $globalvar in foo() because $globalvar is in the global scope and foo() has its own scope, see http://de2.php.net/language.variables.scope

The same is true for bar.php. Since it was included within foo() it shares the scope of that function.
You can either import $globalvar to the scope of the function

Code: Select all

function foo() {
  global $globalvar;
or use $_GLOBAL['globalvar']. The array _GLOBAL is available from anywhere and contains all global variables as elements.

Why does message.php need an object of the class User and why does it include login.php?

Posted: Thu Jun 28, 2007 1:01 pm
by m2babaey
oops! I was comparing PHP with C whereas they are different about this.
account.php is available to members only. when someone wants to visit account.php and he has not logged in, a message should be shown to him asking for login. so I redirect those who have not logged in to message.php.
but I'm trying to manage to have only one page message.php and show all other messages in other parts of the site there by only changing the value of $_SESSION['$file']
I'm not sure which variable I should declare global in the function and which functions. are that functions CheckLogin and MembersOnly or something else?
Thanks

Posted: Thu Jun 28, 2007 8:26 pm
by volka
m2babaey wrote:but I'm trying to manage to have only one page message.php and show all other messages in other parts of the site there by only changing the value of $_SESSION['$file']
$_SESSION['$file']? I only see
m2babaey wrote:$file="login.php";
And why does message.php include login.php on line 70 ?

Posted: Fri Jun 29, 2007 11:17 am
by RobertGonzalez
And get rid of the single quotes around the variable name in the array index. Unless the array index is actually the string '$file'.