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>

Link to comment
Share on other sites

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()"

 

Link to comment
Share on other sites

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.