i have index.php

<form action="login.php" method="post">
			<table width="249">
			<tr>
			<td>
			  <span class="style3">Username:</span>			</td>
			<td>
			<input name="username" type="text" />
			</td>
			</tr>
			<tr>
			<td>
			  <span class="style3">Password:			  </span></td>
			<td>
			<input name="password" type="password" />
			</td>
			</tr>
			<tr>
			<td>
			</td>
			<td>
			<input name="Login" type="submit" value="Login" />
			</td>
			</tr>
			<tr>
			<td colspan="2">
			  <div align="center" class="style3">Not a member yet?<br />
			    Please <a href="register.php">register!</a>			    </div></td>
			</tr>
              </table>  
			</form>

im new in php i have something to ask

what should i do in my login.php to be able to user to login there account.

heres my login.php please check if this is right code or not.

<?php
session_start();
include 'dbconnect.php';
$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "select * from registered_members where username='$username' and password='$password'";

$result = mysql_query($query);


while ($row=mysql_fetch_array($result)) 
 if ($row["password"]==$password ) 
  { 
  
     $_SESSION['username'] = $row['$username'];
  	echo "Login Successful";
}
else
{
echo "bad login";
}
?>

i'm hoping that you can help me :)

you first mus have database

create database member;

CREATE TABLE `user_list` (
   `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
   `username` VARCHAR( 20 ) NOT NULL ,
   `password` VARCHAR( 100 ) NOT NULL ,
) ENGINE = MYISAM ;

and the you must build up register page like that
and then you must connect to database

@mysql_connect(”localhost”, “root”, “your_mysql_pasword”) or die(”Cannot connect to DB!”);
@mysql_select_db(”db_name”) or die(”Cannot select DB!”);


}


   <form action="login.php" method="post">
            <table width="249">
            <tr>
            <td>
              <span class="style3">Username:</span>           </td>
            <td>
            <input name="username" type="text" />
            </td>
            </tr>
            <tr>
            <td>
              <span class="style3">Password:           </span></td>
            <td>
            <input name="password" type="password" />
            </td>
            </tr>
            <tr>
            <td>
            </td>
            <td>
            <input name="Login" type="submit" value="Login" />
            </td>
            </tr>
            <tr>
            <td colspan="2">
              <div align="center" class="style3">Not a member yet?<br />
                Please <a href="register.php">register!</a>               </div></td>
            </tr>
              </table>  
            </form>

and then you must register with help of register.php

i have already a database and a register.php my problem is in my login.php

look the code if that is right or not.

your login.php will look like that

<?php

// Check if he wants to register:
if (!empty($_POST[username]))
{
	// Check if passwords match.
	

	

	// Register him.
	$query = mysql_query("INSERT INTO members 
	(username,  password)
	VALUES	('$_POST[username]','$_POST[password]')
	or die ("Error - Couldn't register user.");
	
	echo "Welcome $_POST[username]! You've been successfully reigstered!<br /><br />
		Please login <a href='index.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%"><h5>Registration</h5></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>Password: <input type="password" name="password" size="25" value="<? echo $_POST[password]; ?>"></label></td>
				</tr>
								<tr>
					<td width="100%"><input type="submit" value="Register!"></td>
				</tr>
			</table>
		</form>
	</body>
</html>

o sorry it is your register.php

in response to your question, your login script should look like this:

<?php
session_start();
include 'dbconnect.php';
$username = $_POST['username'];
$password = md5($_POST['password']);

$query = "select * from registered_members where username='$username' and password='$password'";

$result = mysql_query($query);

if(mysql_num_rows($result) > 0) {
        $row = mysql_fetch_array($result);
        $_SESSION['username'] = $row['$username'];
  	
        echo "Login Successful";
}
else
{
         echo "bad login";
}
?>

you don't need to check password again since it's already been checked when you made your query. If there's a user=$user with a password=$password, then "Login Succesful".

ok
session will start after you logged in
simply add to the header of the page
session_start();
and may write

echo " $_SESSION['username']";

in order to proove
good luck

thanks dampoet i have a follow up question, how can i change my index.php form to welcome $username? is that possible? like here in daniweb if log in above if successful then the form have change to control panel, donate, logout.

if possible and what should i do in my index.php i want to change my form to welcome: username, then have a log out. :) please. and what should be the code in log out.

thanks dampoet i have a follow up question, how can i change my index.php form to welcome $username? is that possible? like here in daniweb if log in above if successful then the form have change to control panel, donate, logout.

if possible and what should i do in my index.php i want to change my form to welcome: username, then have a log out. :) please. and what should be the code in log out.

when you have successfully logged a user to your system by using the code i wrote before you could simply do this on your index.php:

<?
if(isset($_SESSION['username'])){
    echo "Hi ".$_SESSION['username']." <a href=\"logout.php\">Logout</a><br />";
}
else{
   echo '<a href="login.php">Login</a>';
}

that code will greet the user if logged and send login page if not, also will give the option to logout.

Now for your logout.php script use:

<?
if(isset($_SESSION['username'])) unset($_SESSION['username'])

header("Location: index.php");
exit();
?>

nothings happen everytime i log in

<?
if(isset($_SESSION['user'])){
    echo "Hi ".$_SESSION['user']." <a href=\"logout.php\">Logout</a><br />";
}
else
{
   
?>
		
            <form action="login.php" method="post">
			<table width="249">
			<tr>
			<td>
			  <span class="style3">Username:</span>			</td>
			<td>
			<input name="username" type="text" />
			</td>
			</tr>
			<tr>
			<td>
			  <span class="style3">Password:			  </span></td>
			<td>
			<input name="password" type="password" />
			</td>
			</tr>
			<tr>
			<td>
			</td>
			<td>
			<input name="Login" type="submit" value="Login" />
			</td>
			</tr>
			<tr>
			<td colspan="2">
			  <div align="center" class="style3">Not a member yet?<br />
			    Please <a href="register.php">register!</a>			    </div></td>
			</tr>
              </table>  
			</form>
			    <? } ?>

it even not put the username. it's back on the form.

make sure you are using the same value on you SESSION var i noticed you were using username on your login script an now i see user on your greet script.
also make sure you run session_start() before any use of the SESSION var.

in order to make session_start() avaidable for both scripts you should use a common index.php.

i have a problem in log out nothing happens everytime i click log out.

are you using this code:

<?
if(isset($_SESSION['username']))
    unset($_SESSION['username']);
?>

no :) but thanks. this problem is already solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.