-
login area riservata php
ragazzi sto creando un area riservata per il mio social network, solo gli utenti iscritti possono vederlo e ora sto creando un login in php
per raccoglitore i dati nel database
Codice:
idu int(11)
No
auto_increment
username varchar(32) latin1_swedish_ci
No
password varchar(256) latin1_swedish_ci
No
email varchar(256) latin1_swedish_ci
No
-
codice php
Codice PHP:
<?php
// error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
Class Users{
/********************************
SETTING
*********************************/
// le credenziali di accesso al database
private $host_db = 'localhost';
private $user_db = 'root';
private $pass_db = '';
private $name_db = 'test';
// gli url che gestinranno le operazioni di login
public $Urls = array(
'login_page' => 'http://localhost/test/guida_semple/login.php',
'register_page' => 'http://localhost/test/guida_semple/registrazione.php',
'logout_page' => 'http://localhost/test/guida_semple/logout.php'
/*******************************************
se non sai ciò che fai non toccare più nulla
*******************************************/
/*risorse di connessione*/
protected $conn;
protected $selezione_db;
/*variabili di registrazione*/
protected $reg_username;
protected $reg_email;
protected $reg_pass;
protected $reg_confirm_pass;
protected $reg_crypt_pass;
/*variabili di login*/
protected $login_username;
protected $login_password;
protected $login_cryptpass;
protected $login_iduser;
/*variabili per gestire gli errori*/
public $messages = array(
1 => 'Il campo username è obbligatorio.',
2 => 'Il campo email è obbligatorio.',
3 => 'Il campo password è obbligatorio.',
4 => 'Le due password non coincidono.',
5 => 'Il campo username contiene caratteri non validi. Sono consentiti solo lettere, numeri il i seguenti simboli . _ -.',
6 => 'Inserisci una email con sitassi corretta.',
7 => 'La password scelta è eccessivamente breve. Scegli una password di almeno 8 caratteri.',
8 => 'Esiste già un utente registrato con questo username.',
9 => 'Esiste già un utente registrato con questa email.',
10 => 'Registrazione effettuata con successo.',
11 => 'Login errato',
12 => 'Login eseguito con successo.',
13 => 'Logout eseguito con successo.',
14 => 'Per accedere a questa pagina occorre essere loggati.'
public $message_script;
// il costruttore attiva la connessione a mysql
public function __construct(){
$this->connessione();
}
/******************
CONNESSIONE A MYSQL
******************/
protected function connessione(){
$this->conn = mysql_connect($this->host_db, $this->user_db, $this->pass_db) or die(mysql_error());
$this->selezione_db = mysql_select_db($this->name_db, $this->conn) or die(mysql_error());
return TRUE;
}
/*************************************
ALCUNI METODI PER ESEGUIRE VALIDAZIONI
*************************************/
// verifica campo generico non vuoto (TRUE se non vuoto)
public function empty_string($string){
$string = trim($string);
if($string==''){
return TRUE;
}
else{
return FALSE;
}
}
// verifica sintassi username
public function is_username($username){
$regex = '/^[a-z0-9\.\-_]{3,30}$/i';
return preg_match($regex, $username);
}
// verifica sintassi email (TRUE se ok)
public function is_email($email){
$regex = '/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/';
return preg_match($regex, $email);
}
// verifica sintassi password (per semplicità solo lunghezza) (TRUE se ok)
public function is_secure_password($password){
if(strlen($password)>=8){
return TRUE;
}
else{
return FALSE;
}
}
/*****************************************************
METODI PER VERIFICARE ESISTENZA DI USERNAME E PASSWORD
******************************************************/
// verifica esistenza username (TRUE se esiste)
public function isset_username($username){
$query = "SELECT COUNT(username) AS count
FROM users
WHERE username='".mysql_real_escape_string($username)."'
LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['count']==1){
return TRUE;
}
else{
return FALSE;
}
}
// verifica esistenza email (TRUE se esiste)
public function isset_email($email){
$query = "SELECT COUNT(email) AS count
FROM users
WHERE email='".mysql_real_escape_string($email)."'
LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['count']==1){
return TRUE;
}
else{
return FALSE;
}
}
/******************************
I FORM DI LOGIN E REGISTRAZIONE
******************************/
public function get_login_form(){
$html = '
<form action="' .$this->Urls['login_page']. '" method="post" id="form_login">
<fieldset>
<legend>Login<legend>
<label for="login_user">Username</label>
<input type="text" name="username" id="login_user" />
<label for="login_pass">Password</label>
<input type="password" name="pass" id="login_pass" />
<input type="submit" name="login" value="login" id="login_submit"/>
</fieldset>
</form>';
return $html;
}
public function get_register_form(){
$html = '
<form action="' .$this->Urls['register_page']. '" method="post" id="form_register">
<fieldset>
<legend>Registrazione<legend>
<label for="reg_user">Username*</label>
<input type="text" name="username" id="reg_user" />
<label for="reg_email">Email*</label>
<input type="text" name="email" id="reg_email" />
<label for="reg_pass1">Password*</label>
<input type="password" name="pass1" id="reg_pass1" />
<label for="reg_pass2">Confirm Password*</label>
<input type="password" name="pass2" id="reg_pass2" />
<input type="submit" name="register" value="registra" id="reg_submit" />
<input type="reset" name="reset" value="cancella" id="reg_reset" />
</fieldset>
</form>';
return $html;
}
/*****************************
LINK LOGOUT
*****************************/
public function get_link_logout(){
if($this->is_logged()){
return '<a href="'.$this->Urls['logout_page'].'" class="logout">Logout</a>';
}
return '';
}
/*******************************
METODO PER CRIPTARE LE PASSWORD
*******************************/
public function crypt_pass($pass){
return sha1($pass);
}
/*****************************
ESECUZIONE DELLA REGISTRAZIONE
******************************/
public function esegui_registrazione(){
// se il form e i suoi input sono stati inviati
if(isset($_POST['register']) AND
isset($_POST['username']) AND
isset($_POST['email']) AND
isset($_POST['pass1']) AND
isset($_POST['pass2'])){
//valorizziamo alcune variabili
$this->reg_username = trim($_POST['username']);
$this->reg_email = trim($_POST['email']);
$this->reg_pass = trim($_POST['pass1']);
$this->reg_confirm_pass = trim($_POST['pass2']);
// criptiamo la password
$this->reg_crypt_pass = $this->crypt_pass($this->reg_pass);
// eseguiamo la validazione degli input
$valid_input = $this->check_input_registrazione();
// se sono validi
if($valid_input===TRUE){
// inseriemo all'interno del database i dati
$this->query_insert_registrazione();
// settiamo il messaggio di successo della registrazione
$this->message_script = 10;
return TRUE;
}
}
return FALSE;
}
// verifica che gli input siano corretti
protected function check_input_registrazione(){
if($this->empty_string($this->reg_username)){
$this->message_script = 1;
return FALSE;
}
else if($this->empty_string($this->reg_email)){
$this->message_script = 2;
return FALSE;
}
else if($this->empty_string($this->reg_pass)){
$this->message_script = 3;
return FALSE;
}
else if($this->reg_pass != $this->reg_confirm_pass){
$this->message_script = 4;
return FALSE;
}
else if(!$this->is_username($this->reg_username)){
$this->message_script = 5;
return FALSE;
}
else if(!$this->is_email($this->reg_email)){
$this->message_script = 6;
return FALSE;
}
else if(!$this->is_secure_password($this->reg_pass)){
$this->message_script = 7;
return FALSE;
}
else if($this->isset_username($this->reg_username)==TRUE){
$this->message_script = 8;
return FALSE;
}
else if($this->isset_email($this->reg_email)==TRUE){
$this->message_script = 9;
return FALSE;
}
return TRUE;
}
// esecuzione della query insert di registrazione
protected function query_insert_registrazione(){
$query = "
INSERT INTO users
SET
username='".mysql_real_escape_string($this->reg_username)."',
pass='".mysql_real_escape_string($this->reg_crypt_pass)."',
email='".mysql_real_escape_string($this->reg_email)."',
data_reg= NOW()";
$result = mysql_query($query) or die(mysql_error());
return mysql_insert_id();
}
/*******************
ESECUZIONE DEL LOGIN
********************/
public function esegui_login(){
// se il form di login e i sui tutti input sono stati inviati
if(isset($_POST['login']) AND isset($_POST['username']) AND isset($_POST['pass'])){
// valorizziamo delle variabili
$this->login_username = trim($_POST['username']);
$this->login_password = trim($_POST['pass']);
// criptiamo la password
$this->login_cryptpass = $this->crypt_pass($this->login_password);
// validiamo i dati (non devono essere vuoti)
$not_empty_input = $this->check_input_login();
// se la validazione è andata a buon fine
if($not_empty_input===TRUE){
// eseguiamo la query e verifichiamo se individua le credenziali
if($this->query_select_login()==TRUE){
// settiamo lo status di utente loggato
$this->set_logged($this->login_iduser);
// settiamo l'username
$this->set_username($this->login_username);
// settiamo il messaggio di successo del login
$this->message_script = 12;
return TRUE;
}
// se la query non ha trovat utenti con quelle credenziali
else{
// settiamo un messaggio di insuccesso dell'operazone
$this->message_script = 11;
}
}
}
return FALSE;
}
-
Codice PHP:
// verifica che gli input del login non siano vuoti
protected function check_input_login(){
if($this->empty_string($this->login_username)){
$this->message_script = 1;
return FALSE;
}
else if($this->empty_string($this->login_password)){
$this->message_script = 3;
return FALSE;
}
return TRUE;
}
// esecuzione della qeury per verificare il login
protected function query_select_login(){
$query = "
SELECT id FROM users
WHERE
username='".mysql_real_escape_string($this->login_username)."' AND
pass='".mysql_real_escape_string($this->login_cryptpass)."'";
$result = mysql_query($query) or die(mysql_error());
// se individua l'utente
if(mysql_num_rows($result)==1){
$row = mysql_fetch_array($result);
$this->login_iduser = $row['id'];
return TRUE;
}
return FALSE;
}
/***********************************
VERIFICA DELLO STATO DI LOGIN UTENTE
***********************************/
// verifica login
public function is_logged(){
return isset($_SESSION['auth']);
}
// set login
protected function set_logged($id_user){
$_SESSION['auth'] = $id_user;
return;
}
// access denied
public function access_denied(){
if(!$this->is_logged()){
header("location: ".$this->Urls['login_page']."?message=14");
exit;
}
return;
}
protected function set_username($username){
$_SESSION['username_logged'] = $username;
return;
}
public function get_username(){
return isset($_SESSION['username_logged']) ? $_SESSION['username_logged'] : '';
}
// logout
public function logout(){
session_unset();
session_destroy();
setcookie(session_name(), '', time()-42000, '/');
header("location: ".$this->Urls['login_page']."?message=13");
return;
}
/*****************************
METODO PER OTTENERE I MESSAGGI
******************************/
public function get_message(){
if(isset($_GET['message'])){
$this->message_script = $_GET['message'];
}
$key = intval($this->message_script);
if(array_key_exists($key, $this->messages)){
return $this->messages[$key];
}
return FALSE;
}
}
?>
codice php 2
Codice PHP:
<?php
session_start();
require_once('../lib/Users.class.php');
$login = New Users;
/*
PROCESSIAMO LA RICHIESTA AJAX
*/
if(isset($_POST['username'])){
if(!$login->isset_username($_POST['username'])){
echo 'true';
}
else{
echo 'false';
}
}
else if(isset($_POST['email'])){
if(!$login->isset_email($_POST['email'])){
echo 'true';
}
else{
echo 'false';
}
}
?>
grazie se mi aiutate
-
Metti tutto il codice tra i tag così da colorarne la sintassi e facilitarne la lettura...
E poi... che errore ti da?
-
non me lo fa mettere tutto quanto il codice, se no lo facevo. comunque quando un utente si registra appena va nell'area riservata c'è una specie di login che deve fare per vedere il sito privato e non ci riesco
-
Sii chiaro.
Questionario :=D:
- Cosa vorresti che faccia?
- Che cosa invecie fa?
- Ti da un errore? Quale?
-
lascia stare comunque avrei un altro errore nel javascript non mi si vede l'immagine, in html dovrebbe uscire così nella sorgente pagina
<img src="images/icons/smiley/smile.png" align='absmiddle'>
questo è il javascript
<img src='"$.mbEmoticons/smiles/smile.png"+ret+ext+"' align='absmiddle'>
non riesco a fare il collegamento
-
Cosa c'è nella variabile $?
E in ret ed ext?
-
te lo pubblico tutto, grazie mille comunque
// variation from Roberto Bichierai emoticonize component.
jQuery.fn.emoticonize = function (animate) {
function convert (text){
var icons = $.mbEmoticons.getRegExp();
return text.replace (icons,function(str){
var ret= $.mbEmoticons.smilesVariations[str];
var ext=animate?".gif":".png";
if (ret){
ret="<img src='"+$.mbEmoticons.smilesPath+"smiley/"+ret+ext+"' align='absmiddle'>";
return ret;
}else{
return str;
}
});
}
this.each(function() {
var el = $(this);
var html = convert(el.html()).replace(/\n/g,"<br>");
el.html(html);
});
return this;
};
$.extend($.mbEmoticons.smilesVariations,$.mbEmotic ons.smiles);
jQuery.fn.mbSmilesBox= $.mbEmoticons.addSmilesBox;
jQuery.fn.mbOpenSmilesBox= $.mbEmoticons.openSmileBox;
jQuery.fn.mbCloseSmilesBox= $.mbEmoticons.removeSmilesBox;
})(jQuery);
-
Dove è definito $.mbEmoticons?
-
/************************************************** *****************************
jquery.mb.components
Copyright (c) 2001-2011. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
Licences: MIT, GPL
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
************************************************** ****************************/
/*
* Name:jquery.mb.emoticons
* Version: 1.0
*/
(function($) {
jQuery.mbEmoticons= {
author:"Matteo Bicocchi",
version:"1.0",
smilesPath:"",
smiles: {
"(angel)": "angel",
" :@": "angry",
"(bandit)": "bandit",
"(bear)": "bear",
"(beer)": "beer",
" :D": "bigsmile",
"(bow)": "bow",
"(u)": "brokenheart",
"(bug)": "bug",
"(^)": "cake",
"(call)": "call",
"(cash)": "cash",
"(clap)": "clapping",
"(coffee)": "coffee",
" 8-)": "cool",
" ;(": "crying",
"(dance)": "dance",
"(devil)": "devil",
"(doh)": "doh",
"(drink)": "drink",
"(drunk)": "drunk",
"(dull)": "dull",
"(blush)": "eblush",
"(emo)": "emo",
"(envy)": "envy",
" ]:)": "evilgrin",
"(F)": "flower",
"(fubar)": "fubar",
"(giggle)": "giggle",
"(handshake)": "handshake",
"(happy)": "happy",
"(headbang)": "headbang",
"(heart)": "heart",
"(heidy)": "heidy",
"(hi)": "hi",
"(inlove)": "inlove",
"(wasntme)": "itwasntme",
"(kiss)": "kiss",
" :x": "lipssealed",
"(mail)": "mail",
"(makeup)": "makeup",
"(finger)": "middlefinger",
"(mmm)": "mmm",
"(mooning)": "mooning",
"(~)": "movie",
"(muscle)": "muscle",
"(music)": "music",
"(myspace)": "myspace",
" 8-|": "nerd",
"(ninja)": "ninja",
"(no)": "no",
"(nod)": "nod",
"(party)": "party",
"(phone)": "phone",
"(pizza)": "pizza",
"(poolparty)": "poolparty",
"(puke)": "puke",
"(punch)": "punch",
"(rain)": "rain",
"(rock)": "rock",
"(rofl)": "rofl",
" :(": "sadsmile",
"(shake)": "shake",
"(skype)": "skype",
" |-)": "sleepy",
"(smile)": "smile",
"(smirk)": "smirk",
"(smoke)": "smoke",
" :|": "speechless",
"(*)": "star",
"(sun)": "sun",
" :O": "surprised",
"(swear)": "swear",
"(sweat)": "sweating",
"(talk)": "talking",
"(think)": "thinking",
"(o)": "time",
"(tmi)": "tmi",
"(toivo)": "toivo",
" :P": "tongueout",
"(wait)": "wait",
"(whew)": "whew",
"(wink)": "wink",
" :^)": "wondering",
" :S": "worried",
"(yawn)": "yawn",
"(yes)": "yes"
},
smilesVariations: {
":-)": "smile",
":)": "smile"
},
smileBoxBtn:"#smileBoxBtn"
,
getRegExp:function(){
var ret="/";
$.each($.mbEmoticons.smilesVariations,function(i){
var emot= i.replace(/\)/g,"\\)").replace(/\(/g, "\\(").replace(/\|/g, "\\|").replace(/\*/g, "\\*").replace(/\^/g, "\\^");
ret +="("+emot+")|";
});
ret+="/g";
return eval(ret);
},
addSmilesBox:function(){
$(this).each(function(){
var textarea=$(this);
var wrapper=$("<span/>").addClass("mbSmilesWrapper");
textarea.wrapAll(wrapper);
textarea.data("caret",textarea.caret());
textarea.data("smilesIsOpen",true);
var smilesBox=$("<div/>").addClass("mbSmilesBox").hide();
var smilesButton=$("<span/>").addClass("mbSmilesButton").html(":-)").emoticonize();
$.each($.mbEmoticons.smiles,function(i){
var emoticon=$("<span/>").addClass("emoticon").html(i).attr("title",i) ;
smilesBox.append(emoticon);
emoticon.emoticonize().data("emoticon",i);
});
textarea.before(smilesButton);
smilesButton.click(function(){
textarea.mbOpenSmilesBox();
});
textarea.before(smilesBox);
textarea.data("smilesBox",smilesBox);
smilesBox.find(".emoticon").each(function(){
var icon=$(this);
$(this).hover(
function(){
var src= $(this).find("img").attr("src").replace(".png",".g if");
$(this).find("img").attr("src",src);
},
function(){
var src= $(this).find("img").attr("src").replace(".gif",".p ng");
$(this).find("img").attr("src",src);
})
.click(function(){
textarea.insertAtCaret(" "+icon.data("emoticon")+" ");
});
});
textarea.focus();
textarea.caret(textarea.data("caret"));
});
return this;
},
openSmileBox:function(){
var textarea = $(this);
var smilesBox= textarea.data("smilesBox");
var left= (textarea.position().left+(textarea.outerWidth()/2));
smilesBox.css({left:left});
smilesBox.fadeIn();
setTimeout(function(){
$(document).one("click",function(){textarea.mbClos eSmilesBox();});
},100);
},
removeSmilesBox:function(){
$(this).data("smilesIsOpen",false);
var smilesBox= $(this).data("smilesBox");
smilesBox.fadeOut(500);
}
};
jQuery.fn.insertAtCaret = function (tagName) {
return this.each(function(){
if (document.selection) {
//IE support
this.focus();
sel = document.selection.createRange();
sel.text = tagName;
this.focus();
}else if (this.selectionStart || this.selectionStart == '0') {
//MOZILLA/NETSCAPE support
startPos = this.selectionStart;
endPos = this.selectionEnd;
scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + tagName.length;
this.selectionEnd = startPos + tagName.length;
this.scrollTop = scrollTop;
} else {
this.value += tagName;
this.focus();
}
});
};
jQuery.fn.caret = function(pos) {
var target = this[0];
if (arguments.length == 0) { //get
if (target.selectionStart) { //DOM
var pos = target.selectionStart;
return pos > 0 ? pos : 0;
}
else if (target.createTextRange) { //IE
target.focus();
var range = document.selection.createRange();
if (range == null)
return '0';
var re = target.createTextRange();
var rc = re.duplicate();
re.moveToBookmark(range.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
else return 0;
} //set
if (target.setSelectionRange) //DOM
target.setSelectionRange(pos, pos);
else if (target.createTextRange) { //IE
var range = target.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
};
// variation from Roberto Bichierai emoticonize component.
jQuery.fn.emoticonize = function (animate) {
function convert (text){
var icons = $.mbEmoticons.getRegExp();
return text.replace (icons,function(str){
var ret= $.mbEmoticons.smilesVariations[str];
var ext=animate?".gif":".png";
if (ret){
ret="<img src='"+$.mbEmoticons.smilesPath+"smiley/"+ret+ext+"' align='absmiddle'>";
return ret;
}else{
return str;
}
});
}
this.each(function() {
var el = $(this);
var html = convert(el.html()).replace(/\n/g,"<br>");
el.html(html);
});
return this;
};
$.extend($.mbEmoticons.smilesVariations,$.mbEmotic ons.smiles);
jQuery.fn.mbSmilesBox= $.mbEmoticons.addSmilesBox;
jQuery.fn.mbOpenSmilesBox= $.mbEmoticons.openSmileBox;
jQuery.fn.mbCloseSmilesBox= $.mbEmoticons.removeSmilesBox;
})(jQuery);
-
qui ce ne sono tanti di $.mbEmoticons
-
comunque gli smile sono definiti nella chat, tipo quando parli con qualcuno su facebook
codice html
<input id="chat" class="chat-user" placeholder="Scrivere un messaggio..." name="chat">
-
Prova a sostituire
smilesPath:"",
Con
smilesPath:"images/icons/",
(Devi metterci il percorso della cartella smiley
-
quando lo metto non va, e quando premo invio non succede niente :(
-
-
-
E le altre te le prende?
Scrivendo (smile) anziché :) te la prende?
-
non mi prende niente :(
ho messo questo
<img src='"+$.mbEmoticons.smilesPath+"images/icons/"+ret+ext+"' align='absmiddle'>
e qui questo
(function($) {
jQuery.mbEmoticons= {
author:"World Live",
version:"1.0",
smilesPath:"smiley",
smiles: {
"(angel)": "angel",
" :@": "angry",
"(bandit)": "bandit",
-
Se, ad esempio, gli simle sono nella cartella images/icons/smiley
Devi mettere:
Codice PHP:
<img src='"+$.mbEmoticons.smilesPath+ret+ext+"' align='absmiddle'>
e
Codice PHP:
(function($) {
jQuery.mbEmoticons= {
author:"World Live",
version:"1.0",
smilesPath:"images / icons / smiley / ", /*togli gli spazi nelle barrette */
...
-
questo qui mi da errore
<img src='"+$.mbEmoticons.smilesPath+ret+ext+"' align='absmiddle'>
-
-
ora nessun errore comunque non trova le immagini ancora
-
Posso vederlo online?
Sei sicuro che l'indirizzo in smilesPath sia giusto?
EDIT
In smilesPath prova a mettere /themes/plus/images/icons/smiley/
-
certo questo è il mio social network ti devi iscrivere per vederlo http://www.itworldlive.it/
poi una volta che sei iscritto vai sui messaggi poi lo trovi
-
Ok, mi sono iscritto... prova a modificare il smilesPath come ti ho detto...
-
ciò provato ma sempre lo stesso :(
-
non so se farlo in html alla fine, comunque grazie per avermi aiutato. ora scappo ciò troppo sonno
-
http://i60.tinypic.com/2ed8isw.jpg
Mi da un 404 al file del plugin... è giusta la cartella?
-