Classi 4D e 4E SIA: inserire il codice CAPCHA in una pagina Php nel modulo form

PAGINE PER LA CREAZIONE DEL CONTROLLO CAPTCHA



index2.php

<html xmlns="http://www.w3.org/1999/xhtml">
<h2 id="footer">
SIMULAZIONE SCOLASTICA<BR>
Per accedere ai contenuti riservati effettua il Login:</h2>
<form action="protetta2.php" method="POST" onsubmit="return checkform(this);">
<!-- START CAPTCHA --> <br />
<div class="capbox">
<div id="CaptchaDiv">&nbsp;</div>
<div class="capbox-inner">inserisci il numero:<br />
<input id="txtCaptcha" type="hidden" />
<input id="CaptchaInput" name="CaptchaInput" size="15" type="text" /></div>
</div>
<br /><br />
<!-- END CAPTCHA -->
<p style="padding-left: 10px;">Username:<br><input name="username" size="20" type="text"
/></p>
<p style="padding-left: 10px;">&nbsp;Password:<br><input name="password" size="20"
type="password" /></p>
<p style="padding-left: 540px;"><input type="submit" value="Login" /> <input type="reset"
value="Reset" /></p>
</form>
<script type="text/javascript">
// Captcha Script
function checkform(theform){
var why = "";
if(theform.CaptchaInput.value == ""){
why += "- Inserisci il codice captcha.\n";
}
if(theform.CaptchaInput.value != ""){
if(ValidCaptcha(theform.CaptchaInput.value) == false){
why += "- Il codice captcha non corrisponde.\n";
}
}
if(why != ""){
alert(why);
return false;
}
}
var a = Math.ceil(Math.random() * 9)+ '';
var b = Math.ceil(Math.random() * 9)+ '';
var c = Math.ceil(Math.random() * 9)+ '';
var d = Math.ceil(Math.random() * 9)+ '';
var e = Math.ceil(Math.random() * 9)+ '';
var code = a + b + c + d + e;
document.getElementById("txtCaptcha").value = code;
document.getElementById("CaptchaDiv").innerHTML = code;
// Validate input against the generated number
function ValidCaptcha(){
var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
var str2 = removeSpaces(document.getElementById('CaptchaInput').value);
if (str1 == str2){
return true;
}else{
return false;
}
}
// Remove the spaces from the entered and generated code
function removeSpaces(string){
return string.split(' ').join('');
}
</script>
<div align="center">
<h3>&nbsp;</h3>
<p style="padding-left: 480px;"><strong>
<span style="color: #ffffff;">Username: polizia &nbsp;</span> &nbsp;</strong>
<span style="color: #ffffff;"><strong>Password: user1</strong></span>
<p>&nbsp;</p>
<h2>&nbsp;</h2>
<p>&nbsp;</p>
</body>
</html>


protetta2.php
<?php
/*trasferisco le variabili che provengono dal modulo*/
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == "polizia" && $password == "user1") /*definisco l'accesso con user e password
per i clienti*/
/*caso 1: se username e password corrispondono visualizzo il link per continuare*/
{
echo "<br><h2><font color=#009900>Benvenuto nell'area
riservata.</font></h2><center><br><br><a href=index.html>Clicca qui per
continuare.</a></center>"; exit ();
}
/*se user e password non corrispondono in nessuno dei casi precedenti visualizzo il link di ritorno al
form per ritentare l'accesso*/
else
{
echo "<center><h2><font color=#FF0000><h2>Accesso negato.</font></h2><p>Le
credenziali inserite non risultano valide.<p><a href=\"javascript:history.go(-1)\">Clicca qui per
tornare al form e reinserire username e password.</a></center>"; exit();
}
?>

Esercizio realizzato da Anna Franceschetti 5D SIA
http://annafranceschetti5.wixsite.com/annafranceschetti/es-inf-v-d

Commenti

Post popolari in questo blog

Simulazioni di reti (con Cisco Packet Tracer)

Esercizi sulla rappresentazione della virgola mobile IEEE 754 (Floating Point)