SHARE
TWEET

Untitled

a guest Jun 18th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. # db configs
  4. define('DB_USERNAME', '');
  5. define('DB_PASSWORD', '');
  6. define('DB_HOST', '');
  7. define('DB_NAME', '');
  8. define('DB_COLL', 'utf8'); # Türkçe için
  9.  
  10. # user class
  11. class User {
  12.    
  13.     var $connection = null,
  14.         $table = '`users`';
  15.    
  16.     # istenirse tablo değiştirilebilir
  17.     function __construct($table = '') {
  18.         if($table)
  19.             $this->table = $table;
  20.     }
  21.    
  22.     function connect() {
  23.         $this->connection = @mysql_connect(DB_HOST, DB_USER, DB_PASS)
  24.             or die('DB connection failed!');
  25.        
  26.         @mysql_select_db($this->db_name, $this->connection)
  27.             or die('DB selection failed!');
  28.            
  29.         if(DB_COLL)
  30.             @mysql_unbuffered_query("SET NAMES '". DB_COLL ."';");
  31.     }
  32.    
  33.     # yeni kayıt
  34.     function save() {
  35.         $values = array(
  36.             $username = mysql_real_escape_string($_POST['username'], $this->connection),
  37.             $password = md5($_POST['password']),
  38.             // ...
  39.         );
  40.         $values = "'". join("','", $values) . "'";
  41.         @mysql_query('INSERT INTO '. $this->table .' (id, username, password) VALUES (NULL, '. $values .');');
  42.         return mysql_insert_id();
  43.     }
  44.    
  45.     # düzenleme işlemi
  46.     function edit() {
  47.         $id = intval($_POST['id']);
  48.         $values = array(
  49.             $username = "username='". mysql_real_escape_string($_POST['username'], $this->connection) ."'",
  50.             $password = "password='". md5($_POST['password']) ."'",
  51.             // ...
  52.         );
  53.         $values = join(', ', $values);
  54.         @mysql_query('UPDATE '. $this->table .' SET '. $values .' WHERE id='. $id .' LIMIT 1;');
  55.         @mysql_query('OPTIMIZE TABLE'. $this->table .';');
  56.         return mysql_affected_rows();
  57.     }
  58.    
  59.     # silme işlemi
  60.     function delete() {
  61.         $id = intval($_POST['id']);
  62.         @mysql_query('DELETE FROM '. $this->table .' WHERE id='. $id .' LIMIT 1;');
  63.         @mysql_query('OPTIMIZE TABLE'. $this->table .';');
  64.         return mysql_affected_rows();
  65.     }
  66.    
  67.     # listeleme işlemi
  68.     function list($limit) {
  69.         $result = @mysql_query('SELECT * FROM '. $this->table .' ORDER BY id DESC LIMIT '. intval($limit) .';');
  70.         $str = '<table>';
  71.         $str .= '<tr><th>ID</th><th>Username</th><th>Password</th><th>&nbsp;</th></tr>';
  72.         while($user = mysql_fetch_object($result)) {
  73.             $str .= '<tr>';
  74.                 $str .= '<td>'. $user->id .'</td>';
  75.                 $str .= '<td>'. $user->username .'</td>';
  76.                 $str .= '<td>'. $user->password .'</td>';
  77.                 $str .= '<td><a href="javascript:uyeSil('. $user->id .');">Sil</a></td>';
  78.                 $str .= '<td><a href="javascript:uyeDuzenle('. $user->id .');">Düzenle</a></td>';
  79.             $str .= '</tr>';
  80.         }
  81.         $str .= '</table>';
  82.        
  83.         # hafızayı temizle
  84.         @mysql_free_result($result);
  85.        
  86.         return $str;
  87.     }
  88.    
  89.     # bağlatıyı kapat
  90.     function close() {
  91.         if($this->connection !== null) {
  92.             @mysql_close($this->connection);
  93.             $this->connection = null;
  94.         }
  95.     }
  96.    
  97. }
  98.  
  99. # kullanım - ajax.php
  100. if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
  101.  
  102.     # class çağrılır
  103.     $user = new User();
  104.     # db bağlantısı kurulur
  105.     $user->connect();
  106.    
  107.     # dönüş
  108.     $x = null;
  109.    
  110.     # function belirlenir
  111.     $function = $_POST['action'];
  112.     if($function == 'list')
  113.         $x = $user->list($_POST['limit']);
  114.     else {
  115.         $functions = array('save', 'edit', 'delete');
  116.         if(in_array($function, $functions))
  117.             $x = $user->{$function}();
  118.     }
  119.    
  120.     # json formatında cevap
  121.     $response = ($x !== null)
  122.         ? '{"response":'. $x .'}')
  123.         : '{"response":null, "error":true}');
  124.    
  125.     # db bağlantısını kapatılır
  126.     $user->close();
  127.    
  128.     # sonuc bas ve çık
  129.     die($response);
  130.    
  131. }
  132. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top