php client login - extend to user specific pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dirksmith
Forum Newbie
Posts: 11
Joined: Fri Oct 07, 2005 4:19 pm

php client login - extend to user specific pages

Post by dirksmith »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi

I've just successfully installed the php client login and register set-up (http://www.swedesignz.com/php.html?id=7).

I need to extend it to allow a user to login and be directed to their own unique user page. 

So I would like the registration form to include a client specific url input area + then modify the login page to redirect the user 

to that page once logged in + then to logout from that page.

I assume the database + registration.php will require a user homepage component - with the client being directed home page on being logged in. 

Any advice - ideally quite detailed as I am a PHP newbie - on achieving this by modifying the code below would be very much appreciated.

Thanx

Dirk




The pages and code:


//the database

Code: Select all

TABLE `members` (
`id` MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 50 ) NOT NULL ,
`firstname` VARCHAR( 50 ) NOT NULL ,
`lastname` VARCHAR( 50 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
`date` VARCHAR( 50 ) NOT NULL ,
`ip` VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
`username`
//register.php

Code: Select all

<?php

// Check if he wants to register:
if (!empty($_POST[username]))
{
    // Check if passwords match.
    if ($_POST[password] != $_POST[password2])
        exit("Error - Passwords don't match. Please go back and try again.");

    // Assign some variables.
    $date = mktime("d - m - Y");
    $ip = $_SERVER[REMOTE_ADDR];

    require_once("connect.php");

    // Register him.
    $query = mysql_query("INSERT INTO members 
    (username, firstname, lastname, password, date, ip)
    VALUES    ('$_POST[username]','$_POST[firstname]','$_POST[lastname]','$_POST[password]','$date','$ip')")
    or die ("Error - Couldn't register user.");
    
    echo "Welcome $_POST[username]! You've been successfully reigstered!
        Please login <a href='login.php'><b>here</b></a>.";
    exit();
}

?>

<html>
    <head>
        <title>Register</title>
    </head>
    <body>
        <form action="register.php" method="post">
            <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
                <tr>
                    <td width="100%">Registration</td>
                </tr>
                <tr>
                    <td width="100%"><label>Desired Username: <input type="text" name="username" size="25" value="<? echo 

$_POST[username]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>First Name: <input type="text" name="firstname" size="25" value="<? echo $_POST[

firstname]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Last Name: <input type="text" name="lastname" size="25" value="<? echo $_POST[

lastname]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Password: <input type="password" name="password" size="25" value="<? echo 

$_POST[password]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Verify Password: <input type="password" name="password2" size="25" value=""></

label></td>
                </tr>
                <tr>
                    <td width="100%"><input type="submit" value="Register!"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

//login.php

Code: Select all

<?php
session_start();
// Check if he wants to login:
if (!empty($_POST[username]))
{
    require_once("connect.php");

    // Check if he has the right info.
    $query = mysql_query("SELECT * FROM members
                            WHERE username = '$_POST[username]'
                            AND password = '$_POST[password]'")
    or die ("Error - Couldn't login user.");
    
    $row = mysql_fetch_array($query)
    or die ("Error - Couldn't login user.");
    
    if (!empty($row[username])) // he got it.
    {
        $_SESSION[username] = $row[username];
        echo "Welcome $_POST[username]! You've been successfully logged in.";
        exit();
    }
    else // bad info.
    {
        echo "Error - Couldn't login user.
            Please try again.";
        exit();
    }
}

?>

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <form action="login.php" method="post">
            <table width="75%" border="1" align="center" cellpadding="3" cellspacing="1">
                <tr>
                    <td width="100%">Login</td>
                </tr>
                <tr>
                    <td width="100%"><label>Username: <input type="text" name="username" size="25" value="<? echo $_POST[

username]; ?>"></label></td>
                </tr>
                <tr>
                    <td width="100%"><label>Password: <input type="password" name="password" size="25" value=""></label></

td>
                </tr>
                <tr>
                    <td width="100%"><input type="submit" value="Login!"></td>
                </tr>
            </table>
        </form>
    </body>
</html>
//logout

Code: Select all

<?php
    $_SESSION[username] = "";
?>

//membership area

Code: Select all

<?php
session_start();

// Check his status.
if (!empty($_SESSION[username])) // he got it.
{
    echo "You are currently logged in, <b>$_SESSION[username]</b>.";
}
else // bad info.
{
    echo "You are currently <b>NOT</b> logged in.";
}

?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply