7, October 2008

best way to safe data on remote server? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: best way to safe data on remote server?
« previous next »
Pages: [1] Print

Author Topic: best way to safe data on remote server?  (Read 983 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« on: Jun 18, 2007, 10:51:33 AM »

Hi,

I need to safe / store some data id the database on a remote server.

the data is not very sensitive but need to be a little protected (that some kid stay outside Wink)

what the best way to do that? using some encrypted string and post the data via a form

or posting the form via cURL using the password feature?

or something else?



Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8104
41581 credits
Members referred : 3



« Reply #1 on: Jun 18, 2007, 01:18:35 PM »

Using password is always better, but if you need security you should use an https connection or even encode the data.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Current Events + Big Sites = Easy Money
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 18, 2007, 01:35:15 PM »

thanks, but the data is not very sensitive (no need for SSL)


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8104
41581 credits
Members referred : 3



« Reply #3 on: Jun 18, 2007, 01:44:11 PM »

In that case you can just use a curl request with some kind of password, but if there is any sniffer between those computers that could be a trouble Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Current Events + Big Sites = Easy Money
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #4 on: Jul 20, 2007, 03:22:07 AM »

Yes, PHP has a good support for en- and decoding mechanism. Here's a class I've written to perform en- and decryption of strings (The class does also support en-/decoding of files but I stripped because it's not needed in this case):

Code:
<?php
  
class EnDecryption
  
{
      function 
__construct($algorithm 'ofb')
      {
          
$this->td mcrypt_module_open('rijndael-256'''$algorithm'');
      }
      
      function 
__destruct()
      {
          @
mcrypt_generic_deinit($this->td);
          
mcrypt_module_close($this->td);
      }
      
      
public function GenerateIv()
      {
          
$iv mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND);
          
$iv substr(md5(time().$iv), 0mcrypt_enc_get_iv_size($this->td));
          return 
$iv;
      }
      
      
public function EncryptString($data$key, &$iv '')
      {
        if (Empty(
$iv))
          
$iv $this->GenerateIv();
          
          
$key substr(md5($key), 0mcrypt_enc_get_key_size($this->td));
          
mcrypt_generic_init($this->td$key$iv);
          return 
base64_encode(mcrypt_generic($this->td$data));
      }
      
      
public function DecryptString($data$key, &$iv '')
      {
        if (Empty(
$iv))
          
$iv $this->GenerateIv();
          
          
$key substr(md5($key), 0mcrypt_enc_get_key_size($this->td));
          
mcrypt_generic_init($this->td$key$iv);
          return 
mdecrypt_generic($this->td,  base64_decode($data));
      }
  }
?>

« Last Edit: Jul 20, 2007, 11:31:18 AM by Nikolas »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jul 20, 2007, 07:23:53 AM »

nice class I guess this is very usefull for alot of us. Please add some explainig text / desciption and add this as a tutorial here:

http://www.webdigity.com/index.php?action=codex Visit through proxy

(and get a free link to the homepage you entrered in your profile)


Last blog : Upload images for usage in TinyMCE
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #6 on: Jul 20, 2007, 03:43:44 PM »

<?php

//Attention: You need to have mcrypt enabled in the php.ini!

if (!extension_loaded('mcrypt'))
{
  if (strtoupper(substr(PHP_OS,0,3)) == 'WIN')
  {
    if (!@dl('php_mcrypt.dll'))
    {
      die('Could not load php_mcrypt.dll');
    }
  }
  else
  {
    if (!@dl('mcrypt.so'))
    {
      die('Could not load mcrypt.so');
    }
  }
}

$string = 'Hello world!';
$key = 'webdigity.com';

//First you need to create a new instance of the class.
$class = new EnDecryption;

//To generate an IV, you might use GenerateIV but you can leave the following line because EncryptString()
//creates an IV if the third parameter was empty
$iv = $class->GenerateIv();

//To encrypt the string you need to pass EncryptString with the string to encode and your key
//Notice: If you didn't generate an IV and the variable $iv is empty, EncryptString will generate an IV for you
//            The third parameter (IV) is being passed by reference that means the third parameter's variable
//            will be assigned a generated IV.
$encrypted = $class->EncryptString($string, $key, $iv);

//$encrypted does now contain the encrypted string. Let's decode it.
$decoded = $class->DecryptString($encrypted, $key, $iv);

//A final comparision will show if everything worked as expected
if ($string == $decoded)
{
  echo 'Everything seems to work fine.';
}
else
{
  echo 'En-/Decoding failed.';
}

?>

I didn't test this code, so there might be some errors.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8104
41581 credits
Members referred : 3



« Reply #7 on: Jul 20, 2007, 03:51:32 PM »

Instead of this :

Code:
<?php
if (!extension_loaded('mcrypt'))
{
  if (
strtoupper(substr(PHP_OS,0,3)) == 'WIN')
  {
    if (!@
dl('php_mcrypt.dll'))
    {
      die(
'Could not load php_mcrypt.dll');
    }
  }
  else
  {
    if (!@
dl('mcrypt.so'))
    {
      die(
'Could not load mcrypt.so');
    }
  }
}
?>


you can use this : (just a less code version)

Code:
<?php
if (!extension_loaded('mcrypt'))
{
    if (!@
dl(strtoupper(substr(PHP_OS,0,3)) == 'WIN' 'php_mcrypt.dll' 'mcrypt.so'))
      die(
'Could not load mcrypt');
}
?>


Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Current Events + Big Sites = Easy Money
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Jul 20, 2007, 04:42:38 PM »


//Attention: You need to have mcrypt enabled in the php.ini!


that could be a problem for a lot of people with a shared hosting account!


Last blog : Upload images for usage in TinyMCE
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #9 on: Jul 20, 2007, 05:23:05 PM »

Code:
<?php
if (!extension_loaded('mcrypt'))
{
    if (!@
dl(strtoupper(substr(PHP_OS,0,3)) == 'WIN' 'php_mcrypt.dll' 'mcrypt.so'))
      die(
'Could not load mcrypt');
}
?>


Good idea. I didn't use this possibility because this type of if-clauses are slower than "ordinary" if clauses. Loading differences >= 1 second(s) are noticeable if you call it about 1.000.000 times.
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8104
41581 credits
Members referred : 3



« Reply #10 on: Jul 20, 2007, 07:50:17 PM »

No, those ifs are faster than the common if () else statements. I don't remember the site I read it, but there are benchmarks that point to that.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Current Events + Big Sites = Easy Money
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #11 on: Jul 20, 2007, 08:15:33 PM »

No, I read two days ago at freenode.org in #php.net (IRC) that the common if statements were faster. I'll do a benchmark later.
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Jul 20, 2007, 08:21:50 PM »

So Nick, he knocked you out hehe...


Last blog : Upload images for usage in TinyMCE
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6343
38878 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Jul 20, 2007, 08:23:21 PM »

I use both of them depending on how long the code for the conditions is (make it readable)

... I think performance is important for websites with > 200 visitors an hour


Last blog : Upload images for usage in TinyMCE
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6817
Tags : remote server Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: best way to safe data on remote server?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 07, 2008, 07:18:58 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!





Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.