Salut.
Poti testa variantele astea:
Cu PHP:
Cod: Selectaţi tot
<?php
session_start();
// daca exista sesiunea cu codul, il retine in variabila
if(isset($_SESSION['cspm'])) $cspm = $_SESSION['cspm'];
else {
// altfel, creaza niste caractere (numere, litere) aleatorii
$data = date(" j-F-Y, g:i a ");
$cspm = substr(md5($data), 3, 5);
$_SESSION['cspm'] = $cspm; // retine codul in sesiune
}
echo $cspm; // afiseaza codul
?>
Cu JavaScript:
Cod: Selectaţi tot
Cod:
<div id="cspm"></div>
<script type="text/javascript">
function randomCode(nmr) {
// sirul de caractere din care e ales random; si varasbila returnata
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
var randomstring = '';
// alege un numar de "nmr" caractere aleatorii
for (var i=0; i<nmr; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
var rand_code = randomCode(5); // preia codul
document.getElementById('cspm').innerHTML = rand_code; // adauga in tag-ul cu id="cspm"
alert(rand_code); // afiseaza in fereastra Alert
</script>