+ Reply to Thread
Results 1 to 9 of 9

Thread: This code doesn't seem to work...

  1. #1
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    This code doesn't seem to work...

    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:
     <?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;
    }
    ?>
    

    Lambada - the former Account Manager (before I resigned)




  2. #2
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: This code doesn't seem to work...

    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 by Bryon; 01-18-2007 at 11:19 PM.

  3. #3
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: This code doesn't seem to work...

    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
    Lambada - the former Account Manager (before I resigned)




  4. #4
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: This code doesn't seem to work...

    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 Code:
    <?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 by Bryon; 08-12-2010 at 04:05 PM.

  5. #5
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: This code doesn't seem to work...

    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 Code:
    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
    Welcome
    To logout please Click Here *-*Free File Sharing*-*
    So it's working, just not displaying the UserName. Any ideas? Thanks.
    Lambada - the former Account Manager (before I resigned)




  6. #6
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: This code doesn't seem to work...

    Hmm.. Try:

    PHP Code:
    ...
    echo 
    'Welcome '$_SESSION['UserName'];
    ... 
    If that doesn't work, see what's in the session global by using:

    PHP Code:
    print_r($_SESSION); 
    You might be setting $_SESSION['Username'],$_SESSION['userName'], or something similar rather than $_SESSION['UserName'].
    Last edited by Bryon; 01-18-2007 at 11:19 PM.

  7. #7
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: This code doesn't seem to work...

    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 by lambada; 06-02-2006 at 02:06 PM.
    Lambada - the former Account Manager (before I resigned)




  8. #8
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: This code doesn't seem to work...

    Are you starting the session at the top of the page?

  9. #9
    lambada's Avatar
    lambada is offline x10 Elder lambada is an unknown quantity at this point
    Join Date
    Mar 2006
    Location
    Caister, Gt Yarmouth, Norfolk, ENGLAND
    Posts
    1,222

    Re: This code doesn't seem to work...

    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 by lambada; 06-04-2006 at 06:42 AM.
    Lambada - the former Account Manager (before I resigned)




+ Reply to Thread

Similar Threads

  1. Hybrid's HTML Lessons
    By Hybrid in forum Tutorials
    Replies: 18
    Last Post: 11-28-2009, 02:12 PM
  2. Every10Minutes.com code for sale
    By ƒorte in forum The Marketplace
    Replies: 5
    Last Post: 10-30-2005, 11:09 AM
  3. Ad Code Question
    By reiterb in forum Free Hosting
    Replies: 1
    Last Post: 08-18-2005, 09:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers