a

Login with Bootstrap validation via POST

This application uses the sc_url_library function of Scriptcase to include the Bootstrap CSS libraries, and the PHP file_get_contents function to access a webservice created in another application that performs user validation and typed password.

Application Details

1. We are using a Blank application to display the login screen and include the Bootstrap library:

   

2. In the application we have the following code:

Blank "login"

$str_error_msg = "";
// Check if the login and password were typed.
if(isset($_POST['login']) && !empty($_POST['login']) && isset($_POST['password']) && !empty($_POST['password']))
{
// Parameters to be passed to the webservice.
$parms = 'option=validate_login';
$parms .= '&str_login=' . $_POST['login'];
$parms .= '&str_password=' . $_POST['password'];
$webservice_url = dirname(dirname("http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"])) . "/user/";

// Access the webservice.
$str_output = sc_webservice('file_get_contents', $webservice_url, $_SERVER["SERVER_PORT"], "POST", $parms, array(), 30);

// Check the return from the webservice.
if($str_output != 'ok')
{
$str_error_msg = $str_output;
}
else
{
sc_redir('menu');
}
}
// Starts the Login page layout.
?>
<html>
<head>
<title>Bootstrap Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Include the Bootstrap libraries -->
<link rel="stylesheet" type="text/css" href="<?php echo sc_url_library('prj', 'bootstrap-3.3.2-dist', 'css/bootstrap.min.css'); ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo sc_url_library('prj', 'bootstrap-3.3.2-dist', 'css/style1.css'); ?>" />
</head>
<body>


<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4 col-md-offset-4">
<h1 class="text-center login-title">Sign in to continue to Login</h1>
<div class="account-wall">
<img class="profile-img" src="https://lh3.googleusercontent.com/-OJ26yjLQ8mo/AAAAAAAAAAI/AAAAAAAAAHg/9aw8LXIfn5I/s120-c/photo.jpg?sz=120" alt="">
<form class="form-signin" method="post">
<input type="text" name="login" class="form-control" id="form-login" placeholder="Email" required autofocus>
<input type="password" name="password" class="form-control" id="form-password" placeholder="Password" required>
<span role="alert" class="error-msg" id="errormsg_0_Passwd"><?php echo $str_error_msg; ?></span>
<input type="submit" value="Sign in" class="btn btn-lg btn-primary btn-block" />
<label class="checkbox pull-left">
<input type="checkbox" value="remember-me">
Remember me
</label>
<a href="#" class="pull-right need-help">Need help? </a><span class="clearfix"></span>
</form>
</div>
<a href="#" class="text-center new-account">Create an account </a>
</div>
</div>
</div>
</body>
</html>
<?php // Ends the Login page layout.

User validation service application

3. The code above performs access to the application "/user/", which will validate the login and password entered. This application will be a Blank containing the following code:

Blank "user"

if(isset($_POST['option']) && !empty($_POST['option']))
{
// Enter the condition if parameters have been correctly received from the previous application
$str_return = "";

// Check the option clicked by the user.
switch($_POST['option'])
{
case 'validate_login':
// Option to validate user login and password.
sc_lookup(rs, "SELECT COUNT(*) FROM sec_users WHERE login = " . sc_sql_injection($_POST['str_login']) . " AND pswd = " . sc_sql_injection($_POST['str_password']));

if(isset({rs[0][0]}) && {rs[0][0]} > 0)
{
$str_return = "ok";
}
else
{
$str_return = {lang_msg_sec01};
}
break;

case 'add_user':
// Option to insert an user.

break;

case 'delete_user':
// Option to delete an user.

break;
}

echo $str_return;
exit;
}

Untitled Document

Rate this Tutorial
Bad Great
Leave your comments (optional).