Guest User

Untitled

a guest
Sep 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. // dont use passwd through GET, and a file can only be send through POST method :)
  3. // enctype="multipart/form-data <-= use this in the html form
  4. // there is parts you need to change, the mysql db infos like login and passwords, tables and collums...
  5. // review the code, it should have some erros, I dont do anything in php mysql in years ( 4 years )
  6.  
  7. function getRealIpAddr() {
  8.     if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
  9.       $ip=$_SERVER['HTTP_CLIENT_IP'];
  10.     } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
  11.       $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  12.     } else {
  13.       $ip=$_SERVER['REMOTE_ADDR'];
  14.     }
  15.     return $ip;
  16. }
  17.  
  18. function connect_db($user, $pass) {
  19.     $link = mysql_connect('localhost', $user, $pass);
  20.     if (!$link) {
  21.         die('error: '. mysql_error());
  22.     }
  23.     mysql_select_db("db", $link);
  24.     return $link
  25. }
  26.  
  27. function check_login($link, $username, $password) {
  28.     $query = sprintf("SELECT * FROM table WHERE login='%s'",
  29.                         mysql_real_escape_string($username));
  30.     $result = mysql_query($query, $link);
  31.     if ($result) {
  32.         // check user
  33.         $row = mysql_fetch_array($result, MYSQL_ASSOC);
  34.         if ($row['passwd'] == $password) {
  35.             $query = sprintf("UPDATE 'table' SET lastIP="%d" WHERE login='%s'", getRealIpAddr(),
  36.                                 mysql_real_escape_string($username));
  37.             mysql_query($query, $link);
  38.         } else {
  39.             ?> error, incorrect password <?php
  40.         }
  41.     } else {
  42.         // add new user
  43.         $query = sprintf("INSERT INTO table 'table' VALUES \(%s, %s, 0\)",
  44.                             mysql_real_escape_string($username),
  45.                             mysql_real_escape_string($password));
  46.         mysql_query($query, $link);
  47.     }
  48.    
  49. }
  50.  
  51. function update_count_db_upload($link, $username) {
  52.     // retrieve user upload count
  53.     $query = sprintf("SELECT 'count' FROM table WHERE login='%s'",
  54.                         mysql_real_escape_string($username));
  55.     $result = mysql_query($query, $link);
  56.  
  57.     $row = mysql_fetch_array($result, MYSQL_ASSOC);
  58.     $row['count']++;
  59.    
  60.     // update user upload count
  61.     $query = sprintf("UPDATE 'table' SET count="%d" WHERE login='%s'", $row['count']
  62.                         mysql_real_escape_string($username));
  63.     mysql_query($query, $link);
  64. }
  65.  
  66. function save_file($file, $username) {
  67.     $fname = "/uploads/schematics/".addslashes($username)."_filename.schematic";
  68.     move_uploaded_file($file['tmp_name'], $fname);
  69.     echo "done uploading");
  70. }
  71.  
  72. function list_schematics() {
  73.     if ($handle = opendir('/uploads/schematics/')) {
  74.         echo "Directory handle: $handle\n <br />";
  75.         echo "Entries:\n <br />";
  76.  
  77.         /* This is the correct way to loop over the directory. */
  78.         while (false !== ($entry = readdir($handle))) {
  79.             echo "$entry\n <br />";
  80.         }
  81.  
  82.         closedir($handle);
  83.     }
  84. }
  85. // -------------------------------------------------------------
  86. $link = connect_db($db_username,$db_password);
  87.  
  88. $username = $_GET['username'];
  89. $password = $_GET['password'];
  90. $file = $_FILES['filename'];
  91.  
  92. if !empty($username) {
  93.     check_login($link, $username, $password);
  94.     if !empty($file) {
  95.         save_file($file, $username);
  96.         update_count_db_upload($link, $username);
  97.     } else {
  98.         ?> error in .schematic file <?php
  99.     }
  100. } else {
  101.     list_schematics();
  102. }
  103. ?>
Add Comment
Please, Sign In to add comment