Jump to content

[SOLVED] processing login and using session to hold data


Recommended Posts

hi,

 

I am working on a login page for my system. I have a DB all setup which holds client details including the Username and Password.

 

I have this on my login page

 

<?php session_start();?>
<?php include "includes/header.php"; ?>
<div id="page_content">
<div id="sub_menu"></div>
    <div id="content">
    <h1>Welcome</h1>
	<form action="processlogin.php" method="POST">
	<table cellspacing="10">
	<tr><td><p>User ID:</td><td><input class="box" type="text" name="username"></td></tr>
	<tr><td><p>Password:</td><td><input class="box" type="password" name="password"></td></tr>
	<tr><td colspan="2"><input type="submit" value="Login"></td></tr>
	</table>
	</form>
    </div>
</div>		

<?php include "includes/footer.php"; ?>

 

header includes the config.php file and DB connection is all workign fine

 

I then have this on my processlogin.php page

 

<?php 

$username = $_POST["username"];
$password = $_POST["password"];

include "includes/config.php";

$sql="SELECT * FROM clients WHERE username='$username' and password='$password'";
$r = mysql_query($sql);
if(!$r) {
$err=mysql_error();
print $err;
exit();
}
if(mysql_affected_rows()==0){
echo "<p>The username or password you have entered is incorrect!</p>";
echo "<META HTTP-EQUIV=Refresh CONTENT='4; URL=index.php'> ";
exit();
}
else{
$_SESSION["username"] = $username;
echo "<div align='center'><h5>Logging In</h5></div>";
echo "<META HTTP-EQUIV=Refresh CONTENT='2; URL=client_home.php'> ";
}
?>

 

i then have this on my landing page after log in is processed

 

<?php session_start();?>
<?php include "includes/header.php"; ?>
<div id="page_content">
<div id="sub_menu"></div>
    <div id="content">
    <h1>Welcome back <?php echo $_SESSION['username'];?></h1>
    </div>
</div>		

<?php include "includes/footer.php"; ?>

 

after logging in i get the following error

 

Notice: Undefined index: username in C:\wamp\www\fbmcrm\clients\client_home.php on line 6

 

i think the username is not being passed properly in the session. I am trying to acheive a landing page which simply generates a welcome message saying welcome back (username) to start with and the username is specific to the login details used.

 

Any ideas?

 

if(mysql_num_rows() < 1){
echo "<p>The username or password you have entered is incorrect!</p>";
echo "<META HTTP-EQUIV=Refresh CONTENT='4; URL=index.php'> ";
exit();
}
else{
$data = mysql_fetch_assoc($r);
$_SESSION["username"] = $username;
$_SESSION['clientid'] = $data['clientid']; // given that the col name from mysql is that
// more data set here
echo "<div align='center'><h5>Logging In</h5></div>";
echo "<META HTTP-EQUIV=Refresh CONTENT='2; URL=client_home.php'> ";
}
?>

 

Just like that....

now trying to get the full client info from teh DB on teh landing page.

 

Here is the full landing page

 

<?php session_start();?>
<?php include "includes/header.php"; ?>
<div id="page_content">
<div id="sub_menu"></div>
    <div id="content">
    <h1>Welcome back <?php echo $_SESSION['username'];?></h1>
    <?php

$result = mysql_query("SELECT * FROM clients WHERE `id` = '".$_GET['id']."'") 
or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
?>
    <div id="add_client_box">
      <h1>Account Details</h1>
        <table width="404" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="left">Company : </td>
                <td class="right"><?php echo $row['company'];?></td>
            </tr>
            <tr>
                <td class="left">Account No : </td>
                <td class="right"><?php echo $row['account_number'];?></td>
            </tr>
            <tr>
                <td class="left">Website : </td>
                <td class="right"><?php echo $row['website'];?></td>
            </tr>
        </table>      
    </div>
    <div id="add_client_box">
      <h1>Company Address</h1>
	<table width="404" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="left">Building Number : </td>
                <td class="right"><?php echo $row['building_number'];?></td>
            </tr>
            <tr>
                <td class="left">Street : </td>
                <td class="right"><?php echo $row['street'];?></td>
            </tr>
            <tr>
                <td class="left">City : </td>
                <td class="right"><?php echo $row['city'];?></td>
            </tr>
            <tr>
                <td class="left">County : </td>
                <td class="right"><?php echo $row['county'];?></td>
            </tr>
            <tr>
                <td class="left">Post Code : </td>
                <td class="right"><?php echo $row['post_code'];?></td>
            </tr>
        </table>
    </div>
    <div id="add_client_box">
      <h1>Primary Contact</h1>
	<table width="404" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="left">Primary Contact : </td>
                <td class="right"><?php echo $row['primary_contact'];?></td>
            </tr>
            <tr>
                <td class="left">Primary Email : </td>
                <td class="right"><?php echo $row['primary_email'];?></td>
            </tr>
            <tr>
                <td class="left">Primary Phone : </td>
                <td class="right"><?php echo $row['primary_phone'];?></td>
            </tr>
        </table>
    </div>
    <div id="add_client_box">
      <h1>Billing Contact</h1>
      <table width="404" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="left">Billing Contact : </td>
                <td class="right"><?php echo $row['billing_contact'];?></td>
            </tr>
            <tr>
                <td class="left">Billing Email : </td>
                <td class="right"><?php echo $row['billing_email'];?></td>
            </tr>
            <tr>
                <td class="left">Billing Phone : </td>
                <td class="right"><?php echo $row['billing_phone'];?></td>
            </tr>
        </table>
    </div>
    <div id="add_client_box">
      <h1>User Credentials</h1>
      <table width="404" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="left">Username : </td>
                <td class="right"><?php echo $row['username'];?></td>
            </tr>
            <tr>
                <td class="left">Password : </td>
                <td class="right"><?php echo $row['password'];?></td>
            </tr>
        </table>
    </div>
    <div id="add_account_box"></div>

    </div>
</div>		
<?php } ?>
    </div>
</div>		

<?php include "includes/footer.php"; ?>

 

I now get an undefined index again which i assume is because query is wrong or i need to add the ID from teh session somewhere on teh page so it knows what the ID is in the query. Any ideas?

 

Cheers again

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.