PlacementExperience
server.php
Go to the documentation of this file.
1 <?php
20 session_start();
21 error_reporting(0);
22 
23 $firstname = "";
24 $lastname = "";
25 $email = "";
26 $branch = "";
27 $password1 = "";
28 $password2 = "";
29 
30 $error = array();
31 $servername = "localhost";
32 $username = "root";
33 $pass = "";
34 $dbname = "webpage";
35 
36 $db = mysqli_connect($servername, $username, $pass, $dbname);
37 
38 
39 if(isset($_POST['submit'])){
40  $name = mysqli_real_escape_string($db, $_POST['name']);
41  $roll = mysqli_real_escape_string($db, $_POST['roll']);
42  $email = mysqli_real_escape_string($db, $_POST['email']);
43  $branch = mysqli_real_escape_string($db, $_POST['branch']);
44  $password1 = mysqli_real_escape_string($db, $_POST['password1']);
45  $password2 = mysqli_real_escape_string($db, $_POST['password2']);
46 
47 
48  if(empty($name)){array_push($error, "firstname is required");}
49  if(empty($roll)) {array_push($error, "lastname is required");}
50  if(empty($email)){array_push($error, "email is required");}
51  if(empty($branch)){array_push($error, "branch is required");}
52  if(empty($password1)){array_push($error, "password1 is required");}
53  if(empty($password2)){array_push($error, "password2 is required");}
54  if($password1 != $password2){array_push($error, "password must be same !!");}
55 
56 
61  $email_check_query = "SELECT * FROM student WHERE email = '$email' LIMIT 1";
62  $result = mysqli_query($db, $email_check_query);
63  $user = mysqli_fetch_assoc($result);
64  if($user){
65  if($user['email'] == $email){
66  array_push($error, "Email has already been exits");
67 
68  }
69  }
70 
71 
72 
73  if(count($error)==0)
74  {
75  $password = md5($password1);
76  $created = date('Y-m-d H:i:s');
81  $inset_query ="INSERT INTO student(name, roll, email,branch,password,created_at) VALUES ('$name','$roll','$email','$branch','$password','$created')";
82  mysqli_query($db, $inset_query);
83  $_SESSION['email'] = $email;
84  $_SESSION['success'] = "You are now logged in";
85 
86  header("location: index.php");
87 
88 
89  }
90 
91 }
92 if(isset($_POST['login_user'])){
93  $email = mysqli_real_escape_string($db, $_POST['email']);
94  $password = mysqli_real_escape_string($db, $_POST['password']);
95  $err =0;
96  if(empty($email)){
97  array_push($error, "Username is required");
98  header("location: index.php");
99  $err += 1;
100 
101  }
102  if(empty($password)){
103  array_push($error, "password is required");
104  header("location: index.php");
105  $err += 1;
106  }
107 
108 
109  $password = md5($password);
115  $query = "SELECT * FROM student WHERE email = '$email' AND password = '$password'";
116  $result = mysqli_query($db, $query);
117  if(mysqli_num_rows($result) <= 0){
118  array_push($error, "Email doesn't exits");
119  }
120 
121  $row = mysqli_fetch_row($result);
122  $id = $row[0];
123  $name = $row[1];
124  $roll = $row[2];
125 
126 
127  if(count($error) == 0){
128  if(mysqli_num_rows($result) == 1){
129  $_SESSION['Logged_email'] = $email;
130  $_SESSION['id'] = $id;
131  $_SESSION['name'] = $name;
132  $_SESSION['roll'] = $roll;
133  $_SESSION['loggedin'] = '1';
134  echo "Login successfully".$email.$password;
135  header("location: mainpage.php");
136  }
137 
138  }else{
139  header("location: index.php");
140  }
141 
142 
143 }
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 ?>