28, May 2012

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

 
Webdigity webmaster forums
[ 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
Instabuck - The easy way to sell digital products online

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


It's time to use PHP5!


« on: Jun 18, 2007, 09: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?


I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Jun 18, 2007, 12: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 or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Jun 18, 2007, 12:35:15 pm »

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

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Jun 18, 2007, 12: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 or twitter

Last blog : Butterfly Marketing 2.0
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #4 on: Jul 20, 2007, 02: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, 10:31:18 am by Nikolas »
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jul 20, 2007, 06: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

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

Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #6 on: Jul 20, 2007, 02: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: 5799
46391 credits
Members referred : 3



« Reply #7 on: Jul 20, 2007, 02: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 or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Jul 20, 2007, 03: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!

Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #9 on: Jul 20, 2007, 04: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: 5799
46391 credits
Members referred : 3



« Reply #10 on: Jul 20, 2007, 06: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 or twitter

Last blog : Butterfly Marketing 2.0
Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #11 on: Jul 20, 2007, 07: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: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: Jul 20, 2007, 07:21:50 pm »

So Nick, he knocked you out hehe...

Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Jul 20, 2007, 07: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

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....

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?
May 28, 2012, 07:57:56 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!






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