נוצר על ידי admin ב June 10, 2011

בפוסט זה אני אציג קוד שמאפשר לבצע הורדת גיבוי של בסיס הנתונים משרת ה MySQL בהקלקה פשוטה על קובץ BATCH . אם אתם שואלים את עצמכם למה צריך לטרוח ולבצע גיבוי , תקראו את התסריטים הבאים :

א) פורצים לאתר שלכם  ושותלים מידע שלא היה קיים שם קודם לכן (זה עוד נחשב למצב טוב ) ,     או לחלופין מוחקים מידע מטבלאות \ מוחקים טבלאות  \ עושים DROP לבסיס הנתונים  .

ב) החוזה שלכם עם חברת האכסון הסתיים , והם לא מאפשרים לכם להוריד את המידע שהיה קיים בו .

ג) שאילתא שהרצתם הרסה נתונים מסויימים שלא ניתן לשחזר  (בד"כ UPDATE ללא WHERE ).

היתרון של שימוש בקובץ BATCH הוא בכך שאם בניתם אתר ללקוח והוא נדרש לבצע לו גיבוי ,לרוב אין לו מושג מה רוצים ממנו . במקרה כזה סקריפט כזה יכול לעזור לו , ובעיקר לכם :) .

ניתן לתזמן את ביצוע הגיבוי בעזרת משימות מתוזמנות של חלונות או  CRON JOB של שרתי UNIX  . אני מאמין שחברות אחסון מנעו את האפשרות להריץ סקריפטים מסיומת BAT . ולכן תוכלו להריץ את זה מהמחשב שלכם ולכוון את זה לשרת ה SQL שלכם  (יש לאפשר גישה מרוחקת לבסיס נתונים מהכתובת IP שלכם ). באופן כללי יותר  עדיף שהגיבוי לא ישב על אותו שטח אחסון  .

מכיוון שאני לא רוצה שכל גיבוי ידרוס את קודמו , אז הגיבויים נוצרים עם תאריך ושעה בשם שלהם . כדי להשתמש בסקריפט תעתיקו את הקוד הבא לקובץ טקסט ותשמרו אותו בסיומת BAT .

mysqldump -h HOST_NAME -u USER_NAME -p DB_NAME>BACKUP_FILE_NAME_%date:~10%%date:~4,2%%date:~7,2%_%time:~1,1%%time:~3,2%.sql

הערה : אני לא הצבתי את פרמטר הסיסמא בקוד מכיוון שלמרות ההזנה שלה , עדיין נדרשת כתיבתה מחדש .

נוצר על ידי admin ב June 9, 2011

הקוד הבא עוסק בחיבור DataGrid לתוצאות שאילתא שהורצה בשרת  SQL  (בדגש על SQL SERVER ) .  במסגרת עבודתי  נתבקשתי לעשות זאת עבור יישום WPF , ואני זוכר שהחיפוש אחרי מחרוזת החיבור המתאימה והדרך הנכונה לבצע את החיבור גזלו ממני זמן רב . אם אתם נמצאים בשלבי הלימוד הראשונים שלכם אז הקוד הבא יכול לעזור לכם גם מבחינת הבנה וגם מבחינת חיסכון בזמן .

אני שמתי את הקריאה למתודה הזו ב constractor של המחלקה כך שהמילוי הראשוני יתבצע ישר עם עליית הטופס . אבל באותה מידה תוכלו להוסיף כפתור שבמאורע CLICK שלו תתבצע קריאה למתודה .  

public void setDataGridValues() {

    //the connection string
	String cstr = "Integrated Security=SSPI;Persist Security Info=False;";
	       cstr+= "Data Source=SERVER_NAME\\INSTANCE_NAME;User ID=;";
	       cstr+= "Password=;Initial Catalog=DATABASE_NAME";
	//the connection object
	SqlConnection con = new SqlConnection(cstr);
    //the query
	String query = "SELECT * FROM TABLE_NAME";
    //the command whice combines the query and the connection together
	SqlCommand cmd = new SqlCommand(query, con);
    //the data adapter 
	SqlDataAdapter adp = new SqlDataAdapter(cmd);
    //object whice hold data as table pattern
	DataTable dt = new DataTable();
    //fill the data table with data from the data adaptor 
    adp.Fill(dt);
    //binding the datagrid to the datatable
	dataGrid1.DataContext = dt;
           
   }

 

נוצר על ידי admin ב April 10, 2011
הקוד הבא שברצוני לשתף אותכם בו הוא מחלקה שיצרתי בשפת PHP ליצירת CAPTCHA . מכיוון שידעתי שלא לכולכם יש סבלנות להתעסק עם ביצוע הגדרות אז המחלקה מגיעה עם ערכים כברירת מחדל . הערך היחידי שאתם חייבים לשנות הוא שם קובץ הפונט . מכיוון שלא רציתי להפר זכויות יוצרים אז לא השתמשתי בפונט אריאל שמגיע עם חלונות, אלא עם פונט שניתן לשימוש חופשי ברישיון GPL . פונט כזה ניתן למצוא ב פה . למי שכן מעוניין לבצע שינוי הגדרות הדבר אפשרי ואף בניתי מתודות (פונק' במחלקה ) שנועדו למטרה זו .
 
קוד המחלקה :
<?php
   header('Content-Type: image/png');
   session_start();
 
 class Captcha{
   /**
    * the image object
    * @access private
    * @var Object;
    */
   private $Im;
   /**
    * image properties
    * @access private
    * @var Array
    */
   private $Image  = array('Color','Height' => 36,'Width' => 122);
   /**
    * image border properties
    * @access private
    * @var Array
    */
   private $Border = array('Color','Size' => 1);
   /**
    * image string properties
    * @access private
    * @var Array
    */
   private $String = array('Angel' => 0,'Color','Length' => 5,'Text' => '',
                           'X_Pos' => 33,'Y_Pos' => 24);  
   /**
    * string font properties
    * @access private
    * @var Array
    */
   private $Font   = array('Size' => 16,'File' => "3.ttf");
   /**
    * image,border and text rgb colors
    * @access private
    * @var Array
    */
   private $RGB    = array('Image' => '255,255,255','Border' => '0,0,0','Text' => '0,0,0');
   
  /**
   * set the image Height
   * @access public
   * @param int $ImageHeight
   * @return void
   */
  public function set_Image_Height($ImageHeight){
   $this->Image['Height'] = $ImageHeight;
  }
  /**
   * set the image Height
   * @access public
   * @param int $ImageWidth
   * @return void
   */
  public function set_Image_Width($ImageWidth){
   $this->Image['Width'] = $ImageWidth;
  }
  /**
   * set the Border Size
   * @access public
   * @param int $BorderSize
   * @return void
   */
  public function set_Border_Size($BorderSize){
   $this->Border['Size'] = $BorderSize;
  }
  /**
   * set the Border Size
   * @access public
   * @param int tAngle;//The angle in degrees,
   *                   //with 0 degrees being left-to-right reading text
   * @return void
   */
  public function set_String_Angel($String_Angel){
   $this->String['Angle'] = $String_Angel;
  }
  /**
   * set the string length
   * @access public
   * @param int $String_Length
   * @return void
   */
  public function set_String_Length($String_Length){
   $this->String['Length'] = $String_Length;
  }
  /**
   * set the String X_Pos
   * @access public
   * @param int $String_X_Pos
   * @return void
   */
  public function set_String_X_Pos($String_X_Pos){
   $this->String['X_Pos'] = $String_X_Pos;
  }
  /**
   * set the String Y_Pos
   * @access public
   * @param int $String_Y_Pos
   * @return void
   */
  public function set_String_Y_Pos($String_Y_Pos){
   $this->String['Y_Pos'] = $String_Y_Pos;
  }
  /**
   * set the Font Size
   * @access public
   * @param int $FontSize
   * @return void
   */
  public function set_Font_Size($FontSize){
   $this->Font['Size'] = $FontSize;
  }
  /**
   * set the Font File
   * @access public
   * @param int $FontFile
   * @return void
   */
  public function set_Font_File($FontFile){
  /**
   * @var Object ; \\text Font File;
   */
    $this->Font['File'] = $FontFile;
  }
  /**
   * set the Image Color
   * @access public
   * @param int $ImageRgb
   * @return void
   */
  public function set_Image_Color($ImageRgb){
   $this->RGB['Image'] = $ImageRgb;
  }
  /**
   * set the Border Color
   * @access public
   * @param int $BorderRgb
   * @return void
   */
  public function set_Border_Color($BorderRgb){
   $this->RGB['Border'] = $BorderRgb;
  }
  /**
   * set the Text Color
   * @access public
   * @param int $TextRgb
   * @return void
   */
  public function set_Text_Color($TextRgb){
   $this->RGB['Border'] = $TextRgb;
  }
 
  /**
   * Make Border to the Image
   * @access private
   * @return void
   */
  private function MakeBorder(){
    $this->Im = imagecreate($this->Image['Width'], $this->Image['Height']);
 $RGB = explode("," , $this->RGB['Border']);
 $this->Border['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
 imagerectangle($this->Im, 0, 0, $this->Image['Width'],
                $this->Image['Height'], $this->Border['Color'] );
 
  }
  /**
   * make the image
   * @access private
   * @return void
   */
  private function MakeImage(){
   $this->MakeBorder();
 $RGB = explode(",", $this->RGB['Image']);
 $this->Image['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
    imagefilledrectangle($this->Im,$this->Border['Size'] , $this->Border['Size'] ,
                      $this->Image['Width'] -($this->Border['Size'] +1),
       $this->Image['Height']-($this->Border['Size'] +1),
       $this->Image['Color']);
  }
 
  /**
   * make the string for the image
   * @access private
   * @return void
   */
  private function MakeString(){
   $this->MakeImage();
  for ($i=0;$i<$this->String['Length'];$i++){
   /**
    * 224-250 is the range of hebrew letters
    * @var string
    */
  $this->String['Text'] .= iconv("WINDOWS-1255","UTF-8",chr(rand(224,250)));
  }
   }
  /**
   * starts session which contains the image string encripted
   * @access private
   * @return void
   */
  private function MakeSession(){
   $this->MakeString();
   $_SESSION['Captcha'] = hash('sha256',$this->String['Text']);
                            
  }
  /**
   * create the captcha
   * @access public
   * @return void
   */
  public function MakeCaptcha(){
   $this->MakeSession();
   //change the text encoding;
   $this->String['Text'] = iconv( "UTF-8", "WINDOWS-1255",$this->String['Text']);
   $this->String['Text'] = iconv( "WINDOWS-1255", "UTF-8", hebrev($this->String['Text']));
   $RGB = explode(",", $this->RGB['Text']);
   $this->String['Color'] = imagecolorallocate($this->Im, $RGB[0], $RGB[1], $RGB[2]);
   
   //create text in the image from ttf file
   imagettftext($this->Im, $this->Font['Size'],
                $this->String['Angel'], $this->String['X_Pos'],
    $this->String['Y_Pos'], $this->String['Color'],
    $this->Font['File'], $this->String['Text']);
    
   imagepng($this->Im);
   imagedestroy($this->Im);
  }
 
 }
 
?>

כדי להשתמש במחלקה יש להגדיר ככה :

<?php
 
 $cap = new Captcha;
 $cap->MakeCaptcha();

?>

ניתן לשנות את הגדרות הבסיס באופן הבא :

<?php

 $cap = new Captcha;
 $cap->set_Border_Color('200,100,100');
 // here we define the font file name
 $cap->set_Font_File("font_file_name.ttf");
 $cap->set_Border_Size(3);
 $cap->MakeCaptcha();

?>

כדי לבצע את ההשוואה נעשה כך :


<?php

if (hash('sha256',$_REQUEST['User_CaptCha']) === $_SESSION['Captcha'] ){
 //here we process the data if the auth is o.k.
}

?>