Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include("conf-inc.php");
  4.  
  5.  //nak check sama ada username dah digunakan ke belum
  6.  
  7. function usernameTaken($uname){
  8.    global $conn;
  9.    if(!get_magic_quotes_gpc()){
  10.       $uname = addslashes($uname);
  11.    }
  12.    $sql = "select demo_uname from demouser where demo_uname = '$uname'";
  13.    $result = mysql_query($sql,$conn);
  14.    return (mysql_numrows($result) > 0);
  15. }
  16.  
  17.  
  18.  //Inserts data dalam database arr
  19.  
  20. function insertNewUser($demouser){
  21.    global $conn;
  22.    
  23.         $fname          = mysql_escape_string($demouser['demo_fname']);
  24.         $lname          = mysql_escape_string($demouser['demo_lname']);
  25.         $add1           = mysql_escape_string($demouser['demo_add1']);
  26.         $country        = mysql_escape_string($demouser['demo_country']);
  27.         $kod            = mysql_escape_string($demouser['demo_kod']);
  28.         $state          = mysql_escape_string($demouser['demo_state']);
  29.         $email          = mysql_escape_string($demouser['demo_email']);
  30.         $uname          = mysql_escape_string($demouser['demo_uname']);
  31.         $password       = mysql_escape_string($demouser['demo_password']);
  32.         $ip             = $_SERVER['REMOTE_ADDR'];
  33.        
  34.        
  35.    $sql = "INSERT INTO $db.demouser (demo_fname, demo_lname, demo_add1, demo_country, demo_kod, demo_state, demo_email, demo_uname, demo_password, demo_ip ,demo_acct) VALUES ('$fname','$lname','$add1','$country','$kod','$state','$email','$uname','$password','$ip',20)";
  36.    return mysql_query($sql,$conn);
  37. }
  38.  
  39. //fungsi nak display message samaada berjaya or tak
  40.  
  41. function displayStatus(){
  42.    $uname = $_SESSION['reguname'];
  43.    if($_SESSION['regresult']){
  44. ?>
  45.  
  46. <h1>Registered!</h1>
  47. <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now</p>
  48.  
  49. <?
  50.    }
  51.    else{
  52. ?>
  53.  
  54. <h1>Registration Failed</h1>
  55. <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
  56. Please try again at a later time.</p>
  57.  
  58. <?
  59.    }
  60.    unset($_SESSION['reguname']);
  61.    unset($_SESSION['registered']);
  62.    unset($_SESSION['regresult']);
  63. }
  64.  
  65. if(isset($_SESSION['registered'])){
  66.  
  67. //hanya akan kuar bila percubaan utk register arr
  68.  
  69. ?>
  70.  
  71. <html>
  72. <title>Registration Page</title>
  73. <body>
  74.  
  75. <? displayStatus(); ?>
  76.  
  77. </body>
  78. </html>
  79.  
  80. <?
  81.    return;
  82. }
  83.  
  84. //display message
  85. if(isset($_POST['f_submit'])){
  86.    /* Make sure all fields were entered */
  87.    if(!$_POST['uname'] || !$_POST['password']){
  88.       die('You didn\'t fill in a required field.');
  89.    }
  90.  
  91.    //Spruce up username, check length
  92.    $_POST['uname'] = trim($_POST['uname']);
  93.    if(strlen($_POST['uname']) > 30){
  94.       die("Sorry, the username is longer than 30 characters, please shorten it.");
  95.    }
  96.  
  97.    // Check if username is already in use
  98.    if(usernameTaken($_POST['uname'])){
  99.       $use = $_POST['uname'];
  100.       die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
  101.    }
  102.  
  103.    //Tambah akaun baru dalam DB
  104.    $md5pass = md5($_POST['password']);
  105.    $aData = $_POST;
  106.    $aData["password"] = $md5pass;
  107.    $_SESSION['reguname'] = $_POST['uname'];
  108.    $_SESSION['regresult'] = insertNewUser($aData);
  109.    $_SESSION['registered'] = true;
  110.    return;
  111. }
  112.  
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement