/    Sign up×
Community /Pin to ProfileBookmark

Function Issues

Hey,

I thought I would easily be able to force a variable out of a function by making it global, but that didn’t work, and I’ve spent hours on workarounds that have mangled my login script. This is what I want to achieve:

login.php

[code=php]if (_checkLogin($username, $password, $remember) !== true) {
$message = _checkLogin($username, $password, $remember);
} else {
redirect(‘http://www.mydomain.com/account.php’);
}[/code]

login.inc.php (include page with all the functions)

[code=php]if (mysql_num_rows($result) == 1) {
…(this part works)…
return true;
} else if (mysql_num_rows($result) !== 1 && mysql_num_rows($result2) == 1) {
session_defaults();
$error = “You need to confirm your account before you can login. Check your inbox of the email address you provided at signup.”;
return false;
} else {
session_defaults();
$error = “Sorry, but that username/password combination doesn’t exist. Try <a href=”forgotpass.php”>Forgot Password</a>.”;
return false;
}[/code]

but right now I have to have this in order to pass something outside of the function (using return):

[code=php]if (mysql_num_rows($result) == 1) {
…(this part works)…
return true;
} else if (mysql_num_rows($result) !== 1 && mysql_num_rows($result2) == 1) {
session_defaults();
return “You need to confirm your account before you can login. Check your inbox of the email address you provided at signup.”;
} else {
session_defaults();
return “Sorry, but that username/password combination doesn’t exist. Try <a href=”forgotpass.php”>Forgot Password</a>.”;
}[/code]

That way I could just echo $error. I thought this would be simple, but now my script doesn’t work right and I am confusing myself. Any help would greatly be appreciated

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@replicaAug 08.2007 — you could pass in another $error variable into the function, by reference. this would be the easiest since you're not using classes.

I would make an authentication class though. have an ErrorMessage variable that's publicly accessable and populate it when $auth->_checkLogin() is called. something like that.

Passing the variable by reference would work though.

function _checkLogin( $name, $pass, $remember, &$err )

{

$err = 'this will be passed to the calling script.';

}

and in the calling script

$error = '';

$result = _checkLogin( 'chuck', 'superpass', false, $error );

if( ! $result ) echo $error;



just some ideas.
Copy linkTweet thisAlerts:
@KillaH425authorAug 08.2007 — Thanks, that worked, I knew I could make a reference but I wasn't sure when I called it, it would work. Now I don't understand why my login script stopped working because I undid everything that I changed, and I didn't touch the setSession function.

This part is in the _checkLogin script that gets called (I know it works because it returns true)

[code=php]$values = serialize(array($row[0], $row[1], $key));
_setSession($values, $remember);
return true;[/code]


Then there is the _setSession function:

[code=php]function _setSession($values, $remember, $init = true) {//&values
list($id, $username, $cookie) = @unserialize($values);
$_SESSION['uid'] = $id;
$_SESSION['username'] = $username;//$values['username'];//htmlspecialchars($values->username)
$_SESSION['cookie'] = $cookie;//$values['cookie'];
$_SESSION['logged'] = true;
if ($remember == true) {
updateCookie($cookie, true);
}
if ($init == true) {
$session = session_id();
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "UPDATE member SET cookie='$cookie', session='$session', ip='$ip' WHERE id='$id'";
mysql_query($sql);
}
}[/code]
Copy linkTweet thisAlerts:
@KillaH425authorAug 08.2007 — Nevermind I fixed it. I hadn't started the session in the right spot.
×

Success!

Help @KillaH425 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...