Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP and MySQL Log In Sessions

    Hi. I'm trying to learn PHP, and I have a good idea on what to do on some things, but this one I have absolutely no idea. So, please go easy on me.

    Anyways, I'm using a MySQL table called 'manager_users' with three columns. One column is the user name column (user_name), the second is the password column (password), and the third is the e-mail address column (e-mail_address). My code is set up like this, the form is on a page (?page=) and my action for the form on the page is like this (?action=log_in). So, I think I have the log in down, but I'm not sure. I need a way to make sure it's actually logging in, and I need a way to make sure it's logging out. Also, I don't know how I would, but I need it to check the forms so that when a form is left blank, it notifies the user.

    Here's the code:
    PHP Code:
    <?php
    session_start
    ();
    mysql_connect('mysql.medicatedsquid.com''medicatedsquid''18e1ns');
    mysql_select_db('medicatedsquid');
    switch(
    $_GET['page']) {

    /* Home Page/Log In */
    case '';
    if(
    $_SESSION ['logged_in'] = 'false') {
    echo 
    'welcome. <a href="?action=log_out">log out</a>';
    } else {
    echo 
    '<form method="post" action="?action=log_in">
    <table align="center" border="0" cellspacing="10" cellpadding="0">
    <tr>
    <td>User Name:</td>
    <td><input type="text" name="user_name" /></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><input type="text" name="password" />
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="log_in" value="Log In" /></td>
    </tr>
    </table>
    </form>'
    ;
    }
    break;

    }
    switch(
    $_GET['action']) {

    /* Log In Action */
    case 'log_in';
    $user_name $_POST['user_name'];
    $user_name $_SESSION['user_name'];
    $password $_POST['password'];
    $query1 mysql_query("SELECT * FROM manager_users WHERE user_name = '$user_name' and password = '$password'");
    $num_rows1 mysql_num_rows($query1);
    if(
    $num_rows 0) {
    $_SESSION['logged_in'] = 'true';
    } else {
    $_SESSION['logged_in'] = 'false';
    }
    break;

    /* Log Out Action */
    case 'log_out';
    $_SESSION['logged_in'] = 'false';
    session_destroy();
    break;

    }
    ?>
    (Yes, I know, I didn't tab my code.) Thanks in advance!

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    If I understand what you're asking for, you want to know how to determine a user is logged in? To do that, you should add a field in the table like 'logged_in' and set it to true or false or 0 or 1.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would recommend using the statement:
    PHP Code:
    or die(mysql_error()) 
    You could insert this after the switch and ifs of your code.

    As for checking whether the form is filled or not, you should use javascript for that. Add onsubmit to your input tag
    Code:
    <input type="submit" name="log_in" value="Log In" onsubmit="validate()" />
    add this script tag on the header part of your site
    Code:
    <script type="text/javascript">
    function validate() {
    var nameBox = document.forms[0].elements[0]
    var pswdBox = document.forms[0].elements[1]
    if (nameBox.value == "" || pswdBox.value == "") {
    alert("Please fill in all fields!")
    return false
    }
    else {
    return true
    }
    }
    </script>
    Last edited by nervegas87; 06-30-2007 at 01:07 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
    •