Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.65 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', TRUE);
  4.  
  5. if (isset($_POST["submit"])) {
  6.  
  7. include "../hash/PasswordStorage.php";
  8. include "../db/db.php";
  9. include "formular_validation.php";
  10.  
  11. if ((Validation::Validate($_POST["zipcode"])) &&
  12. (Validation::Validate($_POST["city"])) &&
  13. (Validation::Validate($_POST["street"])) &&
  14. (Validation::Validate($_POST["number"])) &&
  15. (Validation::Validate($_POST["country"])) &&
  16. (Validation::Validate($_POST["day"])) &&
  17. (Validation::Validate($_POST["month"])) &&
  18. (Validation::Validate($_POST["year"])) &&
  19. (Validation::Validate($_POST["username"])) &&
  20. (Validation::Validate($_POST["email"])) &&
  21. (Validation::Validate($_POST["password"])) &&
  22. (Validation::Validate($_POST["password_retype"]))) {
  23.  
  24. $firstname = htmlspecialchars($_POST["firstname"]);
  25. Validation::firstnameLength($firstname);
  26. Validation::firstnameNoSpace($firstname);
  27.  
  28. $lastname = htmlspecialchars($_POST["lastname"]);
  29. Validation::lastnameLength($lastname);
  30. Validation::lastnameNoSpace($lastname);
  31.  
  32. $zipcode = htmlspecialchars($_POST["zipcode"]);
  33. Validation::zipcodeOnlyNumbers($zipcode);
  34.  
  35. $city = htmlspecialchars($_POST["city"]);
  36. Validation::cityNoNumbers($city);
  37.  
  38. $street = htmlspecialchars($_POST["street"]);
  39. Validation::streetNoNumbers($street);
  40.  
  41. $houseNumber = htmlspecialchars($_POST["number"]);
  42. Validation::houseNumberOnlyNumbers($houseNumber);
  43.  
  44. $additionaladdress = htmlspecialchars($_POST["additional"]);
  45.  
  46. $country = htmlspecialchars($_POST["country"]);
  47. Validation::countryNoNumbers($country);
  48.  
  49. $DOB = $_POST["day"];
  50. $MOB = $_POST["month"];
  51. $YOB = $_POST["year"];
  52.  
  53. $religion = $_POST["religion"];
  54.  
  55. $username = htmlspecialchars($_POST["username"]);
  56. Validation::usernameUnique($username);
  57. Validation::usernameNoSpace($username);
  58.  
  59. $email = htmlspecialchars($_POST["email"]);
  60. Validation::emailCheck($email);
  61.  
  62. $password = htmlspecialchars($_POST["password"]);
  63. Validation::passwordCheck($password);
  64. Validation::passwordNoSpace($password);
  65.  
  66. $passwordConfirmation = htmlspecialchars($_POST["password_retype"]);
  67.  
  68. if ($password !== $passwordConfirmation) {
  69. echo "<div class="alert alert-danger topbar" id='div17'><strong>Danger!</strong> Passwords do not match!</div>";
  70. }
  71.  
  72. $hash = create_hash($password);
  73.  
  74. if ($stmt = $conn->prepare("INSERT INTO formular (firstname, lastname, zipcode, city, street, additionaladdress, country, username, email, hash, dday, dmonth, dyear, religion, housenumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
  75. $stmt->bind_param("ssissssssssssss", $firstname, $lastname, $zipcode, $city, $street, $additionaladdress, $country, $username, $email, $hash, $DOB, $MOB, $YOB, $religion, $houseNumber);
  76. $stmt->execute();
  77. echo "<div class="alert alert-success topbar" id='div0'><strong>Success!</strong> Account successfully created!</div>";
  78. $stmt->close();
  79. $conn->close();
  80.  
  81. } else {
  82. printf("Errormessage: %sn", $conn->error);
  83. echo "<div class="alert alert-danger topbar" id='div0'><strong>Danger!</strong> Something went wrong with the userinput!</div>";
  84.  
  85. }
  86.  
  87. } else {
  88. echo "<div class="alert alert-danger topbar" id='div18'><strong>Danger!</strong> Not all required fields have been filled!</div>";
  89. }
  90. }
  91. ?>
  92.  
  93. <?php
  94.  
  95.  
  96. class Validation
  97. {
  98. const minLength = 2;
  99. const maxLength = 25;
  100.  
  101. public static function Validate($string)
  102. {
  103. if (!empty($string)){
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109.  
  110. public static function firstnameLength($firstname)
  111. {
  112. $firstnameLength = strlen($firstname);
  113.  
  114. if ($firstnameLength < self::minLength){
  115. echo "<div class="alert alert-warning topbar" id='div1'><strong>Warning!</strong> Your firstname is to short!</div>";
  116. }
  117. elseif ($firstnameLength > self::maxLength) {
  118. echo "<div class="alert alert-warning topbar" id='div2'><strong>Warning!</strong> Your firstname is to long!</div>";
  119. }
  120. }
  121.  
  122. public static function firstnameNoSpace($firstname)
  123. {
  124. preg_replace('/s+/', '', $firstname);
  125. }
  126.  
  127. public static function lastnameLength($lastname)
  128. {
  129. $lastnameLength = strlen($lastname);
  130.  
  131. if ($lastnameLength < self::minLength){
  132. echo "<div class="alert alert-warning topbar" id='div3'><strong>Warning!</strong> Your lastname is to short!</div>";
  133. }
  134. elseif ($lastnameLength > self::maxLength) {
  135. echo "<div class="alert alert-warning topbar" id='div4'><strong>Warning!</strong> Your lastname is to long!</div>";
  136. }
  137. }
  138.  
  139. public static function lastnameNoSpace($lastname)
  140. {
  141. $lastnameNoSpace = preg_replace('/s+/', '', $lastname) ? true : false;
  142. if ($lastnameNoSpace == true) {
  143. echo "<div class="alert alert-warning topbar" id='div5'><strong>Warning!</strong> No whitespace allowed!</div>";
  144. }
  145. }
  146.  
  147. public static function zipcodeOnlyNumbers($zipcode)
  148. {
  149. if (!is_numeric($zipcode)) {
  150. echo "<div class="alert alert-warning topbar" id='div6'><strong>Warning!</strong> Only numeric zipcodes allowed!</div>";
  151. }
  152. }
  153.  
  154. public static function cityNoNumbers($city)
  155. {
  156. $cityNoNumbers = preg_replace('/[0-9]+/', '', $city) ? true : false;
  157.  
  158. if ($cityNoNumbers == true) {
  159. echo "<div class="alert alert-warning topbar" id='div7'><strong>Warning!</strong> No numbers allowed in cites!</div>";
  160. }
  161. else {
  162.  
  163. }
  164. }
  165.  
  166. public static function streetNoNumbers($street)
  167. {
  168. if (preg_replace('/[0-9]+/', '', $street)) {
  169. echo "<div class="alert alert-warning topbar" id='div8'><strong>Warning!</strong> No numbers allowed in streets!</div>";
  170. }
  171. }
  172.  
  173. public static function houseNumberOnlyNumbers($numbers)
  174. {
  175. if (!is_numeric($numbers)) {
  176. echo "<div class="alert alert-warning topbar" id='div9'><strong>Warning!</strong> Only numeric housenumbers are allowed!</div>";
  177. }
  178. }
  179.  
  180. public static function countryNoNumbers($country)
  181. {
  182. if (preg_replace('/[0-9]+/', '', $country)){
  183. echo "<div class="alert alert-warning topbar" id='div10'><strong>Warning!</strong> No numbers allowed in countries!</div>";
  184. }
  185. }
  186.  
  187. public static function usernameNoSpace($username)
  188. {
  189. if (preg_replace('/s+/', '', $username)){
  190. echo "<div class="alert alert-warning topbar" id='div11'><strong>Warning!</strong> No spaces allowed in username!</div>";
  191. }
  192. }
  193.  
  194. public static function usernameUnique($username)
  195. {
  196. $sql = "SELECT username FROM log_reg WHERE username = '".mysqli_real_escape_string($username)."'";
  197. if ($sql) {
  198. echo "<div class="alert alert-danger topbar" id='div12'><strong>Danger!</strong> Username is already taken!</div>";
  199. }
  200. }
  201.  
  202. public static function emailCheck($email)
  203. {
  204. if (filter_var($email, FILTER_VALIDATE_EMAIL)){
  205. echo "<div class="alert alert-danger topbar" id='div13'><strong>Danger!</strong> Email is not valid!</div>";
  206. }
  207. }
  208.  
  209. public static function passwordNoSpace($password)
  210. {
  211. if (preg_replace('/s+/', '', $password)) {
  212. echo "<div class="alert alert-danger topbar" id='div14'><strong>Danger!</strong> No spaces allowed in password!</div>";
  213. }
  214. }
  215.  
  216. public static function passwordCheck($password)
  217. {
  218. if (preg_match( '/[^A-Za-z0-9]+/', $password)){
  219. echo "<div class="alert alert-danger topbar" id='div15'><strong>Danger!</strong> Your password must have at least 1 <br>uppercase letter, 1 lowercase letter and 1 number!</div>";
  220. }
  221.  
  222. if(strlen($password) < 8)
  223. {
  224. echo "<div class="alert alert-danger topbar" id='div16'><strong>Danger!</strong> Your password must have at least 8 characters!</div>";
  225. }
  226. }
  227. }
  228.  
  229. <?php
  230.  
  231. $servername = "localhost";
  232. $dbuser = "root";
  233. $dbpass = "secret";
  234. $dbname = "log_reg";
  235.  
  236. $conn = new mysqli($servername, $dbuser, $dbpass, $dbname);
  237.  
  238. if ($conn->connect_error) {
  239. die("Connection failed: " . $conn->connect_error);
  240. }
  241.  
  242. ?>
  243.  
  244. <!Doctype html>
  245. <html>
  246. <head>
  247. <title>Registration</title>
  248. <!-- Latest compiled and minified CSS -->
  249. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  250.  
  251. <!-- Optional theme -->
  252. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  253.  
  254. <!-- Latest compiled and minified JavaScript -->
  255. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  256.  
  257. <style>
  258. /*body, html {
  259. background: url("http://orig06.deviantart.net/4c80/f/2012/266/1/8/pixel_galaxy_by_mrmcintyremedia-d5fn6n3.jpg") no-repeat center center;
  260. color: whitesmoke;
  261. }*/
  262.  
  263. .topbar {
  264. position: absolute;
  265. }
  266.  
  267. /*Success!*/
  268. #div0 {
  269. top: 10%;
  270. left: 40%;
  271. }
  272.  
  273. /*Warning!*/
  274. #div1{
  275. float:left;
  276. top: 17%;
  277. left: 5px;
  278. }
  279. #div2{
  280. float: left;
  281. top: 24%;
  282. left: 5px;
  283. }
  284. #div3{
  285. float: left;
  286. top: 33%;
  287. left: 5px;
  288. }
  289. #div4{
  290. float: left;
  291. top: 40%;
  292. left: 5px;
  293. }
  294. #div5{
  295. float: left;
  296. top: 47%;
  297. left: 5px;
  298. }
  299. #div6{
  300. float: left;
  301. top: 54%;
  302. left: 5px;
  303. }
  304. #div7{
  305. float: left;
  306. top: 61%;
  307. left: 5px;
  308. }
  309. #div8{
  310. float: left;
  311. top: 68%;
  312. left: 5px;
  313. }
  314. #div9{
  315. float: left;
  316. top: 75%;
  317. left: 5px;
  318. }
  319. #div10{
  320. float: left;
  321. top: 82%;
  322. left: 5px;
  323. }
  324. #div11 {
  325. float: left;
  326. top: 89%;
  327. left: 5px;
  328. }
  329.  
  330. /*Danger!*/
  331.  
  332. #div12 {
  333. float: right;
  334. top: 17%;
  335. right: 5px;
  336. }
  337. #div13 {
  338. float: right;
  339. top: 24%;
  340. right: 5px;
  341. }
  342. #div14 {
  343. float: right;
  344. top: 31%;
  345. right: 5px;
  346. }
  347. #div15 {
  348. float: right;
  349. top: 38%;
  350. right: 5px;
  351. }
  352. #div16 {
  353. float: right;
  354. top: 45%;
  355. right: 5px;
  356. }
  357. #div17 {
  358. float: right;
  359. top: 52%;
  360. right: 5px;
  361. }
  362. #div18 {
  363. top: 10%;
  364. left: 40%;
  365. }
  366. </style>
  367. </head>
  368. <body>
  369. <nav class="navbar navbar-inverse">
  370. <div class="container-fluid">
  371. <div class="navbar-header">
  372. <a class="navbar-brand" href="../index.php">log-reg-user-test</a>
  373. </div>
  374. <ul class="nav navbar-nav">
  375. <li class="active"><a href="#">Registration</a></li>
  376. <li class="dropdown">
  377. <a class="dropdown-toggle" data-toggle="dropdown" href="#">Home
  378. <span class="caret"></span>
  379. </a>
  380. <ul class="dropdown-menu">
  381. <li><a href="../home/forum.php">Forum</a></li>
  382. <li><a href="../home/news.php">News</a></li>
  383. <li><a href="../home/blog.php">Blog</a></li>
  384. </ul>
  385. </li>
  386. <li><a href="../log_reg/login.php">Login</a></li>
  387. <li><a href="../log_reg/userarea.php">Restricted Area</a></li>
  388. </ul>
  389. </div>
  390. </nav>
  391. <div class="container">
  392. <h2>Registration</h2>
  393. <br><br>
  394. <form method="post">
  395. <br>
  396. <div class="row">
  397. <div class="col-md-6">
  398. <div class="form-group">
  399. <label for="firstname">Firstname:</label>
  400. <input type="text" class="form-control" id="firstname" placeholder="Firstname" name="firstname">
  401. </div>
  402. </div>
  403. <div class="col-md-6">
  404. <div class="form-group">
  405. <label for="lastname">Lastname:</label>
  406. <input type="text" class="form-control" id="lastname" placeholder="Lastname" name="lastname">
  407. </div>
  408. </div>
  409. </div>
  410. <br><hr><br>
  411. <div class="row">
  412. <div class="col-md-2">
  413. <label for="zipcode">Zipcode:*</label>
  414. <div class="form-group">
  415. <input class="form-control" type="text" id="zipcode" placeholder="zipcode" name="zipcode">
  416. </div>
  417. </div>
  418. <div class="col-md-3">
  419. <div class="form-group">
  420. <label for="city">City:*</label>
  421. <input class="form-control" type="text" id="city" placeholder="City" name="city">
  422. </div>
  423. </div>
  424. <div class="col-md-3">
  425. <div class="form-group">
  426. <label for="street">Street:*</label>
  427. <input class="form-control" type="text" id="street" placeholder="Street" name="street">
  428. </div>
  429. </div>
  430. <div class="col-md-1">
  431. <div class="form-group">
  432. <label for="street">Number:*</label>
  433. <input class="form-control" type="text" id="number" placeholder="Nr." name="number">
  434. </div>
  435. </div>
  436. <div class="col-md-3">
  437. <div class="form-group">
  438. <label for="additionaladdress">Additionaladdress:</label>
  439. <input class="form-control" type="text" id="aditional_address" placeholder="Additionaladdress" name="additional">
  440. </div>
  441. </div>
  442. </div>
  443. <div class="row">
  444. <div class="col-md-3">
  445. <div class="form-group">
  446. <label for="country">Country:*</label>
  447. <input class="form-control" type="text" id="country" placeholder="Country" name="country">
  448. </div>
  449. </div>
  450. <div class="form-group col-md-5">
  451. <label class="control-label">Date of birth:*</label>
  452. <div class="form-group">
  453. <div class="col-md-4">
  454. <select name="day" type="text" class="form-control" id="day" >
  455. <option>--Day--</option>
  456. <?php
  457. for ($dayOfBirth = 1; $dayOfBirth <= 31; $dayOfBirth++) {
  458. echo '<option value="'.$dayOfBirth.'">'.$dayOfBirth.'</option>';
  459. }
  460. ?>
  461. </select>
  462. </div>
  463. <div class="col-md-4">
  464. <select name="month" type="text" class="form-control" id="month">
  465. <option>--Month--</option>
  466. <?php
  467. for ($monthOfBirth = 1; $monthOfBirth <= 12; $monthOfBirth++) {
  468. echo '<option value="'.$monthOfBirth.'">'.$monthOfBirth.'</option>';
  469. }
  470. ?>
  471. </select>
  472. </div>
  473. <div class="col-md-4">
  474. <select name="year" type="text" class="form-control" id="year">
  475. <option>--Year--</option>
  476. <?php
  477. for ($yearOfBirth = 1900; $yearOfBirth <= 2016; $yearOfBirth++) {
  478. echo '<option value="'.$yearOfBirth.'">'.$yearOfBirth.'</option>';
  479. }
  480. ?>
  481. </select>
  482. </div>
  483. </div>
  484. </div>
  485. <div class="col-md-2">
  486. <label for="religion">Religion:</label>
  487. <select type="text" class="form-control" id="religion" name="religion">
  488. <option>--Religion--</option>
  489. <option>Christianity</option>
  490. <option>Islam</option>
  491. <option>Irreligion</option>
  492. <option>Hinduism</option>
  493. <option>Buddhism</option>
  494. <option>Folk religions</option>
  495. </select>
  496. </div>
  497. </div>
  498.  
  499. <br><hr><br>
  500. <div class="row">
  501. <div class="col-md-6">
  502. <div class="form-group">
  503. <label for="username">Username:*</label>
  504. <input type="text" class="form-control" id="username" placeholder="Choose a username" name="username">
  505. </div>
  506. </div>
  507. <div class="col-md-6">
  508. <div class="form-group">
  509. <label for="email">Email:*</label>
  510. <input type="email" class="form-control" id="email" placeholder="Enter a valid email" name="email">
  511. </div>
  512. </div>
  513. </div>
  514. <div class="row">
  515. <div class="col-md-6">
  516. <div class="form-group">
  517. <label for="pwd">Password:*</label>
  518. <input type="password" class="form-control" id="password" placeholder="Password" name="password">
  519. </div>
  520. </div>
  521. <div class="col-md-6">
  522. <div class="form-group">
  523. <label for="pwd">Password-Retype:*</label>
  524. <input type="password" class="form-control" id="password_retype" placeholder="Password-Retype" name="password_retype">
  525. </div>
  526. </div>
  527. </div>
  528. <br><hr><br>
  529. <button type="submit" class="btn btn-default" name="submit">Submit</button>
  530. </form>
  531. </div>
  532. </body>
  533. </html>
  534.  
  535. CREATE TABLE `formular` (
  536. `id` INT(11) NOT NULL AUTO_INCREMENT,
  537. `firstname` VARCHAR(25) NOT NULL DEFAULT '',
  538. `lastname` VARCHAR(25) NOT NULL DEFAULT '',
  539. `zipcode` VARCHAR(10) NOT NULL DEFAULT '',
  540. `city` VARCHAR(30) NOT NULL DEFAULT '',
  541. `street` VARCHAR(30) NOT NULL DEFAULT '',
  542. `housenumber` VARCHAR(10) NOT NULL DEFAULT '',
  543. `country` VARCHAR(30) NOT NULL DEFAULT '',
  544. `dday` VARCHAR(2) NOT NULL DEFAULT '',
  545. `dmonth` VARCHAR(2) NOT NULL DEFAULT '',
  546. `dyear` VARCHAR(4) NOT NULL DEFAULT '',
  547. `religion` VARCHAR(20) NOT NULL DEFAULT '',
  548. `additionaladdress` VARCHAR(30) NOT NULL DEFAULT '',
  549. `username` VARCHAR(30) NOT NULL DEFAULT '',
  550. `email` VARCHAR(30) NOT NULL DEFAULT '',
  551. `hash` VARCHAR(500) NOT NULL DEFAULT '',
  552. `date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  553. `date_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  554. PRIMARY KEY (`id`)
  555. )
  556. COLLATE='utf8_general_ci'
  557. ENGINE=InnoDB
  558. ;
  559.  
  560. if ($stmt = $conn->prepare("INSERT INTO formular (firstname, lastname, zipcode, city, street, additionaladdress, country, username, email, hash, dday, dmonth, dyear, religion, housenumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
  561. $stmt->bind_param("ssissssssssssss", $firstname, $lastname, $zipcode, $city, $street, $additionaladdress, $country, $username, $email, $hash, $DOB, $MOB, $YOB, $religion, $houseNumber);
  562. $stmt->execute();
  563. echo "<div class="alert alert-success topbar" id='div0'><strong>Success!</strong> Account successfully created!</div>";
  564. $stmt->close();
  565. $conn->close();
  566.  
  567. } else {
  568. printf("Errormessage: %sn", $conn->error);
  569. echo "<div class="alert alert-danger topbar" id='div0'><strong>Danger!</strong> Something went wrong with the userinput!</div>";
  570.  
  571. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement