-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
225 lines (187 loc) · 8.63 KB
/
index.php
File metadata and controls
225 lines (187 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
session_start();
require 'db.php';
// Directory where the uploaded files will be saved
$uploadDir = 'uploads/';
$name=$email=$password=$address=$file=$phonenumber='';
$nameErr=$emailErr=$passwordErr=$fileErr=$phonenumberErr='';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// previously i am using this method
// $name=$_POST['name'];
// $email=$_POST['email'];
// $password=$_POST['password'];
// $address=$_POST['address'];
// $file=$_FILES['file'];
// $phonenumber=$_POST['phonenumber'];
////handling the input data of name
if (empty($_POST['name'])) {
$nameErr = 'Name is required.';
} else {
$name = input_data($_POST['name']);
if (!preg_match('/^[a-zA-Z\s]*$/', $name)) {
$nameErr = 'Only alphabets and spaces are allowed.';
}
}
////handling the input data of email
if (empty($_POST['email'])) {
$emailErr = 'Email is required.';
} else {
$email = input_data($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = 'Invalid email format.';
}
}
////handling the input data of password
if(empty($_POST['password'])){
$passwordErr= 'password is required.';
}else{
$password=md5($_POST['password']);
}
////handling the input data of address
if(empty($_POST['address'])){
$addressErr= 'address is empty.';
}else{
$address=$_POST['address'];
}
////handling the input data of phone number
if(empty($_POST['phonenumber'])){
$phonenumberErr='Mobile no. is required.';
}else{
$phonenumber=input_data($_POST['phonenumber']);
if(!preg_match("/^[0-9]*$/",$phonenumber)){
$phonenumberErr= "only numeric numbers are allowed.";
}
if(strlen($phonenumber)!=10){
$phonenumberErr= "Mobile no. must be eqaul to 10 digits.";
}
}
////handling the input data of file
if (isset($_FILES['file']) && $_FILES['file']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$fileNameCmps = explode(".", $fileName);
$fileExtension = strtolower(end($fileNameCmps));
// Sanitize file name
$newFileName = md5(time() . $fileName) . '.' . $fileExtension;
// Create the uploads directory if it doesn't exist
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// Directory where the uploaded file will be moved
$destPath = $uploadDir . $newFileName;
$file=$newFileName;
// Move the file to the destination directory
if (move_uploaded_file($fileTmpPath, $destPath)) {
$_SESSION['message'] = 'File is successfully uploaded.';
} else {
$fileErr = 'There was some error moving the file to upload directory.';
}
} else {
$fileErr = 'There was some error in the file upload. Error code: ' . $_FILES['file']['error'];
}
$created=date("Y-m-d H:i:s");
$sql="INSERT INTO users (Name,Email,password,Address,Phone,Photo,created) VALUES ('$name', '$email', '$password', '$address', '$phonenumber','$file','$created')";
////it checks error if their is error then no login page will be shown after registration
if($nameErr || $passwordErr|| $fileErr || $phonenumberErr ||$emailErr ){
$_SESSION['error']= 'Error occurred while creatting the user.';
}else{
if($conn->query($sql)===TRUE){
$_SESSION['success']='Successfully User Created.';
header("Location: login.php");
exit();
}
}
}
function input_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register Page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light p-4">
<a class="navbar-brand" href="index.php">MyWebsite</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="login.php">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php">Register</a>
</li>
</ul>
</div>
</nav>
<?php
//echo "<p>".(($_SESSION['error'])?($_SESSION['error']):(""))."</p>";
?>
<!-- form html -->
<h1 class="text-center mt-5">Registration Form</h1>
<div class="container p-5">
<form method="post" action="" enctype="multipart/form-data">
<div class="form-group mb-3">
<label for="exampleInputName">Name</label>
<input type="text" name="name" class="form-control mt-1" id="exampleInputName"
aria-describedby="NameHelp" placeholder="Enter Name.." required>
<span class="error" style="<?php echo $nameErr?"display:visible;":"display:none;"?>">
<?php echo "$nameErr"?>
</span>
</div>
<div class="form-group mb-3">
<label for="exampleInputEmail">Email</label>
<input type="email" name="email" class="form-control mt-1" id="exampleInputEmail"
placeholder="Email Address.." required>
<span class="error" style="<?php echo $emailErr?"display:visible;color:red;":"display:none;"?>">
<?php echo "$emailErr"?>
</span>
</div>
<div class="form-group mb-3">
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" class="form-control mt-1" id="exampleInputPassword"
placeholder="Password.." required>
<span class="error" style="<?php echo $passwordErr?"display:visible;color:red;":"display:none;"?>">
<?php echo "$passwordErr"?>
</span>
</div>
<div class="form-group mb-3">
<label for="exampleFormControlTextarea3">Address</label>
<textarea name="address" class="form-control mt-1" id="exampleFormControlAddress" rows="7"></textarea>
</div>
<div class="form-group mb-3">
<label for="exampleInputPhoneNumber">Phone Number</label>
<input type="tel" name="phonenumber" class="form-control mt-1" id="exampleInputPhoneNumber"
placeholder="Phone Number.." required>
<span class="error" style="<?php echo $phonenumberErr?"display:visible;color:red;":"display:none;"?>">
<?php echo "$phonenumberErr"?>
</span>
</div>
<div class="form-group mb-3 mt-4">
<label for="exampleFormControlFile1">file input</label>
<input type="file" name="file" class="form-control-file mt-1" id="exampleFormControlFile1" required>
<span class="error" style="<?php echo $fileErr?"display:visible;color:red;":"display:none;"?>">
<?php echo "$fileErr"?>
</span>
</div>
<button type="submit" class="btn btn-primary mt-4">Save & Register</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>