Results 1 to 4 of 4

Thread: [RESOLVED] login help

  1. #1

    Thread Starter
    Hyperactive Member ninjanutz's Avatar
    Join Date
    Jun 2005
    Location
    Bayside
    Posts
    256

    Resolved [RESOLVED] login help

    on my site i have a blog message board thing and i wanted to no how i could only have users be able to add to the blog instead of everyone

    thanx

  2. #2

    Thread Starter
    Hyperactive Member ninjanutz's Avatar
    Join Date
    Jun 2005
    Location
    Bayside
    Posts
    256

    Re: login help

    i no it involves a hidden field of the username but i need a cookie to keep them logged in

  3. #3

    Thread Starter
    Hyperactive Member ninjanutz's Avatar
    Join Date
    Jun 2005
    Location
    Bayside
    Posts
    256

    Re: login help

    ??? anyone ???

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: login help

    Try using soemthing like the following

    register.php
    PHP Code:
    <?
    if($_POST['action']=="register")
    {
        $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
        msql_select_db('database',$link);
        $query = "INSERT INTO member (username,password)VALUES('{$_POST['username']}','" . md5($_POST['password']). "')"; //create the new user
        mysql_query($query,$link);
    ?>
    Thank you for registering <?=$_POST['username']?>. You can now login.
    <?
    }
    else
    {
    ?>
    <form method="post">
        <table class="columns" cellspacing="0" cellpadding="0">
        <tr>
            <th class="column">Login</th>
        </tr>
            <tr>
                <td>Username
                    <input type="text" name="username" class="text"/>
                </td>
            </tr>
            <tr>

                <td>Password
                    <input type="password" name="password" class="text"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="hidden" name="action" value="reg" />
                    <input type="submit" name="submit" value="Register" class="button" />
                </td>
            </tr>
        </table>
    </form>
    <?
    }
    ?>
    checklogin.php
    PHP Code:
    <?
    session_start();
    function check_user_and_password($username,$password)
    {
        $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
        msql_select_db('database',$link);
        $query = "SELECT username,password FROM member WHERE username = '$username' AND password = '" . md5($password) . "'";
        mysql_query($query,$link);
        if(!empty($results)) //if the query returns nothing then they havent logged in correctly
        {
            $_SESSION['username'] = $username;  //set a value to check later
            $_SESSION['loggedin'] = true; 
            return true;
        }
        else
        {
            return false;
        }
    }
    ?>
    yourscript.php
    PHP Code:
    <?
    require_once('checklogin.php');
    if($_SESSION['loggedin'] == true) //check tosee if they are already logged in
    { //display the memeber stuff
    ?>
        Your memeber stuff here!
    <?
    }
    else
    {
        if($_POST['action'] == "login") // check to see if they are trying to login
        {
             check_user_and_password($_POST['username'],$_POST['password']);
             header('Location: ' $_SERVER['PHP_SELF']); //refresh so user sees member section
        }
        else // they are not tryin to login so make them
        {
        ?>
        <form method="post">
        <table class="columns" cellspacing="0" cellpadding="0">
        <tr>
            <th class="column">Login</th>
        </tr>
            <tr>
                <td>Username
                    <input type="text" name="username" class="text"/>
                </td>
            </tr>
            <tr>

                <td>Password
                    <input type="password" name="password" class="text" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="hidden" name="action" value="login" />
                    <input type="submit" name="submit" value="Login" class="button" />
                </td>
            </tr>
        </table>
    </form>
        <?
        }
    }
    ?>
    It is really basic but it will get you started on what you want to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width