This code doesn't seem to work...

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
The aim of this piece of work under construction is to use a users phpBB login to see if they are an admin and if so give them certain actions (I haven't coded the actions yet)
But, I have a problem - it displays the login form when it should, but when it displays the page afterwards it shows the submitted username and password, but not the user and pass gatherd from the db and yet I can't see where I've gone wrong. I think it's a db retrieval problem. For those who don't know i'm using the PEARL DB module which was recently installed on the free server per my request.

Any help pertaining to this or otherwise is greatly appreciated.

Code:
[COLOR=#000000] <?php
function auth_check($UserName, $UserPass) {
//Connect to database
require 'DB.php';
$db = DB::connect('mysql://lambada_admin:****@localhost/lambada_main');
//Check db connection
if (DB::isError($db)) {die("Can't connect:" . $db->getMessage()); }

//Connection Ended, Data Retrieval Started
$q = $db->query('SELECT username, user_password FROM phpbb_users WHERE username LIKE ?',
                array($UserName));
return $q;
}


//Check for Action requested
switch ($_GET[Action] ) {

//If User is Logging In - denoted by ?Action=LoginProcess
case "LoginProcess":
echo "Processing Login...";
echo "<br />";
auth_check($_GET[UserName], $_GET[UserPass]);
$dbUserName = $q[username];
$dbUserPass = $q[user_password];
echo "submited username =" . $_GET[UserName];
echo "<br />";
echo "submitted userpass = " . $_GET[UserPass];
echo "<br />";
echo "<br />";
echo "dbUserName =" . $q[username];
echo "<br />";
echo "dbUserPass =" . $q[user_password];
break;

//If Invalid or Missing Action then display login form
default:
?>
<html>
<head><title>Ministerial Login BETA</title></head>
<body>
<form method="GET" action="<?php $_SERVER['PHP_SELF']; ?>">
Forum User Name: <input type="text" name="UserName" />
<br />
Forum Password: <input type="password" name="UserPass" />
<input type="hidden" name="Action" value="LoginProcess" />
<br />
<input type="submit" value="Login" />
</form>
<?php include('http://staff.x10hosting.com/adCode.php?ad=advanced'); ?> 
</body>
</html>
<?php
break;
}
?>
[/COLOR]
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Hmm.. I think I see where you went wrong in here.

The function you have, authCheck(), returns an array with the user's information pulled from the database using the two arguments you pass to it. I believe the problem is here:

Code:
auth_check($_GET[UserName], $_GET[UserPass]);
$dbUserName = $q[username];
$dbUserPass = $q[user_password];

I believe you are passing the correct information to the database and it's pulling it from the database fine, but, youre not passing the information back to your script correctly.

You returned the array $q in your function, but in your main script your not putting teh functions return value into the variable $q, thus not making it accessible to the rest of your script.

Try using:
Code:
$q = auth_check($_GET[UserName], $_GET[UserPass]); // You need to put the return value of the function into the varaible $q to be able to access it.
$dbUserName = $q[username];
$dbUserPass = $q[user_password];

I hope you understand what I mean. :)
 
Last edited:

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
Right, I see what you mean.

I didn't realise variables in a function are entirely seperate from variables in the rest of the script.

NOw to wait for http to get back up so I can see if it works.
Thanks :)
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Haha, yea.. When you return a variable from a function it's not "automatically" placed into the same variable in the main script. Complete different "namespace" I guess you could call them.
You *could* do soemthing like:
PHP:
<?php

function sayHello($name) {
  global $string;  // Put the variable $string into the global scope
  $string = 'Hello '. $name .'! How are you?';
  return $string;
}

$name = 'Sir Elston';  // Just a variable holding whatever
sayHello($name);  // Call function
echo $string;  // Would echo 'Hello Sir Elston! How are you?'

?>

You can see I put the variable $string into the global scope in the function, then had the function edit it, then echo'ed it after I called the function. If you want me to clarify anything anymore then I have, just let me know.
 
Last edited:

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
Right, that all worked fine thanks :D

Now it's interpolating data from a session into a string. HEre is the part of code pertaing:

PHP:
case "Home":

if($_SESSION['UserName'] == "")
{echo "ERROR: NOT LOGGED IN";
}
else {
echo "Welcome {$_SESSION['UserName']}";
echo "<br />";
echo "To logout please <a href=\"index.php\">Click Here</a>";
include('http://staff.x10hosting.com/adCode.php?ad=advanced');
}
break;

As you can see I'
ve checked to make sure the session variable exists so I know it's there, but it just displasys

So it's working, just not displaying the UserName. Any ideas? Thanks. :)
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Hmm.. Try:

PHP:
...
echo 'Welcome '. $_SESSION['UserName'];
...

If that doesn't work, see what's in the session global by using:

PHP:
print_r($_SESSION);

You might be setting $_SESSION['Username'],$_SESSION['userName'], or something similar rather than $_SESSION['UserName'].
 
Last edited:

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
The echo 'Welcome '. $_SESSION['UserName']; didn't work, and then using print_r($_SESSION); displayed no new information.

EDIT: Whats even odder is now if I try it, I get the error not logged in message, which means i'm failing that validation test I added. Yet I haven't done anything since i posted it :s
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Are you starting the session at the top of the page?
 

lambada

New Member
Messages
2,444
Reaction score
0
Points
0
Yes I am, atleast I can't see any output in the source of the page before where there should be, and everything before that is in case statements of functions which don't get run.

JUst to check though: here is the full source of the page: http://samwal.x10hosting.com/minlogin/index.phps

It's rather large so I decided not to post it all.

EDIT: I just noticed you haven't configured the free server for phps extentions. :( If you want it in a different format, just ask.
 
Last edited:
Top