we have just started learning PHP as a course in our 2nd year subject Internet Application development. below are the codes that we have worked on so far.

Homepage :

<HTML>
	<HEAD>
	</HEAD>

	<BODY bgcolor='yellow'>
		<P align='center'><FONT color='blue' size='6'>ABC & Company</FONT></P>
	</BODY>
</HTML>

Login.php :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#99FF00" >
	<form action="validateUserLogin.php" method="post">
		User Name <input type="text" name="textBox1" />  <br>
		Password <input type="password" name="txtPassword" /> <br>
		<input type="submit" value="Ok" />
		<input type="button" value="Cancel" />
	</form>
</body>
</html>

User Login Validation:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#FFFF00">
	<p><font color="#FF0000" size="+2">Validating the user login...</font></p>
	
	<!-- Access the incoming username, password values -->
		<!--access the incoming data -->
	<?php
		$s1 = $_POST['textBox1'];
		$s2 = $_POST['txtPassword'];
		
		//echo $s1;
		//echo '<BR>';
		//echo $s2;
	?>
	
	<!-- check/compare those values with the database values -->
	<?php
	$link = mysql_connect('localhost', 'root', '');
	if (!$link) {
		die('Could not connect: ' . mysql_error());
	}
	//echo 'Connected successfully';
	
	// make foo the current db
	$db_selected = mysql_select_db('abc', $link);
	if (!$db_selected) {
		die ('Can\'t use abc : ' . mysql_error());
	}
	
	$query = "SELECT UserName, Password FROM t_user WHERE UserName='$s1' AND Password='$s2'";

	// Perform Query
	$result = mysql_query($query);

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	/*
	if (!$result) {
		echo 'Invllied user login details';
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	} else {
		echo 'Successfull login, welcome to the Sales site';
	}*/
	
	echo '<BR>';
	
	if ($row = mysql_fetch_assoc($result)) {
		//echo $row['UserName'];
		echo '<BR>';
		echo 'Successfull login, welcome to the Sales site';
		//echo $row['Password'];
	} else {
		echo '<BR>';
		echo 'Invllied user login details';
	}

	mysql_close($link);
	?> 
	
	<!-- if valied username, password then show the welcome page -->	
	
	<!-- if not(invalied) show error message -->
</body>
</html>

Welcome Page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#FFFF00">
	<marquee><P><font color="#FF0000" size="2">Welcome to ABC</font></P></marquee>	
	
	<P align="center"><font color="#0000FF" size="+2"><B> Main Page </B></font></P>
</body>
</html>

please let me know how i connect all these. i just have the code but dont know how to connect to mySQL, how do you compare user logins with a database etc. i need urgent help.
Thanks so much

Dani AI

Generated

Good start, — you already have the pieces (homepage, form, validation file). To make this work reliably and safely you need three things: a real users table in MySQL, server-side code that uses prepared statements, and a session to keep the user logged in. Note that the old mysql_* functions seen in the thread are obsolete in modern PHP; use PDO or MySQLi and hashed passwords instead.

A minimal, safe workflow (high level)

  • Create a users table and insert an account (store the password with password_hash()).
  • On login, read the posted username/password, fetch the stored hash with a prepared statement, then call password_verify() to check it.
  • If verified, session_start() and set a session value (regenerate the session id), then redirect to your welcome page.

Sample SQL to create the table:

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(100) NOT NULL UNIQUE,
  password_hash VARCHAR(255) NOT NULL
);

Example PDO-style login flow (conceptual):

// create a PDO connection (use a non-root DB account)
$pdo = new PDO(..., [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT id, password_hash FROM users WHERE username = ?');
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password_hash'])) {
  session_start();
  session_regenerate_id(true);
  $_SESSION['user_id'] = $user['id'];
  header('Location: welcome.php'); exit;
}

Practical tips and cautions: confirm your form name attributes match what PHP reads, ensure MySQL is running and the DB/credentials are correct, do not use MD5 (use password_hash/password_verify), and avoid using the root MySQL account for your app. Use phpMyAdmin or the MySQL client to create the DB/table (as suggested). For details, see the PHP docs on password_hash, PDO and session_start.

You can just put login.php in your homepage. In the user validation maybe will be look like this :

<?php
session_start();
mysql_connect("localhost","root","");
mysql_select_db("abc");
$username=$_POST["username"];
$password=md5($_POST["password"]);
$cek="select * from login
where username=’$username’ and password=’$password’";
$jalankan=mysql_query($cek);

if (mysql_num_rows($jalankan)){
$_SESSION["member"]=$username;
header("Location:welcomepage.php");
}
else{
echo "Failoed to login!";
}
?>

I hope that will help you.

u should go to php myadmin then create your own database and the creat a table which will keep the username and password fields.

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.