Jump to content

New to PHP : Need Help to create a login page using classes


ssuresh07

Recommended Posts

Hi,

I'm new to PHP, I have just started to develop an application.

 

I want to use Classes to built a login page.

 

I'm here posting my coding which is incomplete.

 

Someone give idea of completing it with handling sessions and how to redirect to another page.

 

and when pressing back button it should not goto login page again.

 

<?

class Login

{

  public function __construct($username, $password)

  {

require_once("Connection/db.php");

session_start();

    $this->username = $username;

    $this->password = $password;

    $this->Validate();

  }

  public function __destruct()

  {

  }

  protected function Validate()

  {

  $Query  = "SELECT * FROM master_login WHERE username = ". $txtUserName ."";

$Execute = mysql_query($Query) or die('Query failed1: ' . mysql_error());

$rs        =mysql_fetch_row($Execute);

 

if($rs[1] == $txtUserName && $rs[2] == $txtPassword)

header('Location: index1.html');

  }

}

 

$objLogin = new Login("Guest","guest");

?>

<html>

<head>

<title>Test</title>

</head>

 

<body>

<form name = "frmlogin" method = "post" action="index1.html">

<table align="center">

<tr>

<th><a href="index1.html">Login</a></th>

</tr>

<tr>

<td><input type = "text" name = "txtUserName" /></td>

</tr>

<tr>

<td><input type = "text" name = "txtPassword" /></td>

</tr>

<tr>

<td>

<input type = "submit" name = "submit" value = "Submit" />

<input type = "reset" name = "reset" value = "Reset" />

</td>

</tr>

</body>

</html>

1.

 

if($rs[1] == $txtUserName && $rs[2] == $txtPassword)

{

      $_SESSION['uname']=$txtUserName;

      header('Location: index1.html');

}

================

Then on the login page write the following code on the top

 

2.

 

if($_SESSION['uname'])

{

    header('location:index1.html')

}

 

===================

 

Modify the code posted by you by point 1.

We have just taken a session variable & stored the user name in that.

 

Write the code point no. 2 on the above of the login page.

This will check if a user is logged in or not. i.e username session variable is set or not.

If it is set it wil not show login & redirect it to the  index1.html

 

---- Dont foget to start the session on each page i.e "session_start()"

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.