Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index.php
- <div id="view" class="login-box animated fadeInUp">
- <div class="box-header">
- <h2>Login</h2>
- </div>
- <form name="form" class="form" method="POST">
- <label for="username">Username</label>
- <br/>
- <input type="mail" id="username" name="user_email">
- <input name="submit" onclick="submitForm()" id="submit" type="submit" value="Login">
- </div>
- index.js
- /* login submit */
- function submitForm(){
- var data = $(".form").serialize();
- $.ajax({
- type : 'POST',
- url : '../Slide_upload/database/signIn.php',
- data : data,
- success : function(response){
- if(response=="ok"){
- alert("Ok");
- }
- else{
- alert("Nopeeeeee");
- }
- }
- });
- return false;
- }
- dbConnection.php
- <?php
- class Database{
- private $host = "localhost";
- private $db_name = "slide_uploader";
- private $username = "";
- private $password = "";
- public $conn;
- public function dbConnection(){
- $this->conn = null;
- try{
- $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $exception){
- echo "Connection error: " . $exception->getMessage();
- }
- return $this->conn;
- }
- }
- ?>
- class.student.php
- <?php
- require_once('dbConnection.php');
- class STUDENT{
- private $conn;
- public function __construct(){
- $database = new Database();
- $db = $database->dbConnection();
- $this->conn = $db;
- }
- public function runQuery($sql){
- $stmt = $this->conn->prepare($sql);
- return $stmt;
- }
- public function register($uname,$matricola,$nome,$cognome){
- try{
- $stmt = $this->conn->prepare("INSERT INTO studente(Nome,Cognome,Matricola,Username)
- VALUES($nome, $cognome, $matricola, $uname)");
- $stmt->bindparam(":user_email", $uname);
- $stmt->execute();
- return $stmt;
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- }
- public function doLogin($uname){
- try{
- $stmt = $this->conn->prepare("SELECT * FROM studenti_in_sessione WHERE Username=:user_email");
- $stmt->execute(array(':user_email'=>$uname));
- $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
- if($stmt->rowCount() == 1){
- $exit = array_values($userRow);
- $this->register($exit[0],$exit[1],$exit[2],$uname);
- $_SESSION['user_session'] = $userRow[$this->getStudID($uname)];
- }
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- }
- public function getStudID($uname){
- try{
- $stmt = $this->conn->prepare("SELECT ID_Studente WHERE Username='.$uname' ");
- $stmt->bindparam(":user_email", $uname);
- $stmt->execute();
- return $stmt;
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- }
- public function is_loggedin(){
- if(isset($_SESSION['user_session'])){
- return true;
- }
- }
- public function redirect($url){
- header("Location: $url");
- }
- public function doLogout(){
- session_destroy();
- unset($_SESSION['user_session']);
- return true;
- }
- }
- ?>
- signIn.php
- <?php
- session_start();
- require_once("class.student.php");
- $login = new STUDENT();
- if($login->is_loggedin()!=""){
- $login->redirect('upload.php');
- }
- if(isset($_POST['submit'])){
- $uname = strip_tags($_POST['user_email']);
- if(filter_var($uname, FILTER_VALIDATE_EMAIL)===true && strpos(explode('@',$uname),"studio.unibo.it")!==false){
- if($login->doLogin($uname)){
- sendCode($username);
- }
- else{
- $error = "Non hai i permessi necessari per accedere!";
- }
- }else{
- $error = "La mail deve essere @studio.unibo.it";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement