This post is for login code in Symfony Framework. Here is a simple PHP Login Code!
This is very simple login script written for symfony framework. This will ask for loginid and password like any login page. After successful login it will send user to list page. If unsuccessful, this script will send back to login page and show user error message. If successful, it store user's Name and userid for use in displaying custom messages.
This is very simple login script written for symfony framework. This will ask for loginid and password like any login page. After successful login it will send user to list page. If unsuccessful, this script will send back to login page and show user error message. If successful, it store user's Name and userid for use in displaying custom messages.
Below is login form.
Page::loginSuccess.php
url: /login.html
<?php
if ($sf_flash->has('msg')) {
echo $sf_flash->getRaw('msg');
}
?>
<h1> <center> Login Page</center></h1>
<?php include_partial('global/top_menu');?>
<?php echo form_tag('@prepareLogin', array('method'=>'post', 'id'=>'frm1')); ?>
<table>
<tr>
<th> UserName: </th>
<td> <?php echo input_tag('username', '', array('size'=>20, 'maxsize'=>50)) ?> </td>
</tr>
<tr>
<th> Password: </th>
<td> <?php echo input_password_tag('password', '', array('size'=>20, 'maxsize'=>50));?> </td>
</tr>
<tr>
<td colspan="2" align="center"> <?php echo submit_tag('Submit', ''); ?> </td>
</tr>
</table>
</form>
<?php
//var_dump(@$res);
//echo $user . ": " . $pass;
// echo '<strong> Welcome!, '. $sf_user->getAttribute('authorName') . '</strong>';
echo "<p>". @$msg ."</p>";
?>
<?php if ($sf_request->hasErrors()): ?>
<p>The data you entered seems to be incorrect.
Please correct the following errors and resubmit:</p>
<ul>
<?php foreach($sf_request->getErrors() as $name => $error): ?>
<li><?php echo $name ?>: <?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Now this is actions required to complete the login process. Two actions are used - executeLogin and executePrepareLogin. Action executeLogin will present login form and executePrepareLogin will handle login process after form submittion.
Page::actions.class.php
<?php
public function executeLogin() {
sfView::SUCCESS;
}
public function executePrepareLogin()
{
if ($this->getRequest()->getMethod() != sfRequest::POST) {
sfView::SUCCESS;
}
else {
$usr = $this->getRequestParameter('username');
$pwd = $this->getRequestParameter('password');
$pwd = md5($pwd);
$c = new Criteria();
$c->add(AuthorPeer::USERNAME, $usr);
$c->add(AuthorPeer::PASSWORD, $pwd);
$results = AuthorPeer::doSelect($c);
if ($results) {
foreach ($results as $author) {
$this->setFlash('msg', 'Login Successful.');
$this->getUser()->setAuthenticated(true);
$this->getUser()->setAttribute("logged", true);
$this->getUser()->setAttribute("authorName", $author->getName());
$this->getUser()->setAttribute("authorId", $author->getId());
// Forward
$this->redirect ('main/list'); // module + '/' + action (listSuccess.php)
}
}
else {
$this->setFlash('msg', 'Username/Password is wrong. Try Again!');
$this->getRequest()->setError('userName', 'The user Name field cannot be left blank');
//$this->redirect ('main/login');
$this->forward ('main', 'login');
}
}
return sfView::NONE;
}
?>
Application's config folder>routing.yml:
login_page:
url: /login
param: { module: main, action: login }
prepareLogin:
url: /pLogin
param: { module: main, action: prepareLogin }
# 1 - by Severin
The askeet example shows how validation of username and password can go nicely into the model using the validation system!
See askeet day 6 under “Authenticate a user”
# 2 - by Severin
The askeet example shows how validation of username and password can go nicely into the model using the validation system!See askeet day 6 under “Authenticate a user”
# 3 - by Satya Prakash Karan
yes, and you can use sfGuard Plugin for good login system and adding credential.
# 4 - by Satya Prakash Karan
yes, and you can use sfGuard Plugin for good login system and adding credential.