cookie'yide okumuyor :S hostum olmadığı için deneyemedim.Varsa hatası bildirirsiniz düzeltiriz. Ve bu yöntemi kullanmak için openssl'e ihtiyaç vardır. yolunu belirtmelisiniz.Bu arada aklıma geldi bir konu açsak bu konu altında güvenlik önlemleri ile ilgili kullandığımız fonksiyonları göstersek. Çünkü db'ye giren veri ile dosya yazdırmak için kullanılan veriye farklı önlemler alınır hepsinde aynı yöntemi uygulayamayız istenmeyen bug'lara sebebiyet vereceğinden.
OpenSSL.php
<?php
mysql_connect("localhost", "kullaniciadi", "kullanicisifre") or die ('Mysql bağlantısı yapılamadı');
mysql_select_db("veritabaniismi") or die ('Veritabanı bulunamadı');
DEFINE("OPEN_SSL_CONF_PATH", "c:/apache2triad/opssl/bin/openssl.cnf"); //point to your config file
DEFINE("OPEN_SSL_CERT_DAYS_VALID", 365*5);//5 year
DEFINE("OPEN_SSL_IS_FILE", 1);
class OpenSSL{
var $privatekey; //resource or string private key
var $publickey; //ditto public
var $plaintext;
var $crypttext;
var $ekey; //ekey - set by encryption, required by decryption
var $privkeypass; //password for private key
var $csr; //certificate signing request string generated with keys
var $config;
function OpenSSL(){
$this->config = array("config" => OPEN_SSL_CONF_PATH);
}
function readf($path){
//return file contents
$fp=fopen($path,"r");
$ret=fread($fp,8192);
fclose($fp);
return $ret;
}
//privatekey can be text or file path
function set_privatekey($privatekey, $isFile=0, $key_password=""){
if ($key_password) $this->privkeypass=$key_password;
if ($isFile)$privatekey=$this->readf($privatekey);
$this->privatekey=openssl_get_privatekey($privatekey, $this->privkeypass);
}
//publickey can be text or file path
function set_publickey($publickey, $isFile=0){
if ($isFile)$publickey=$this->readf($publickey);
$this->publickey=openssl_get_publickey($publickey);
}
function set_ekey($ekey){
$this->ekey=$ekey;
}
function set_privkeypass($pass){
$this->privkeypass=$pass;
}
function set_plain($txt){
$this->plaintext=$txt;
}
function set_crypttext($txt){
$this->crypttext=$txt;
}
function encrypt($plain=""){
if ($plain) $this->plaintext=$plain;
openssl_seal($this->plaintext, $this->crypttext, $ekey, array($this->publickey));
$this->ekey=$ekey[0];
}
function decrypt($crypt="", $ekey=""){
if ($crypt)$this->crypttext=$crypt;
if ($ekey)$this->ekey=$ekey;
openssl_open($this->crypttext, $this->plaintext, $this->ekey, $this->privatekey);
}
function do_csr(
$countryName = "TR",
$stateOrProvinceName = "İstanbul",
$localityName = "Bahçelievler",
$organizationName = "Lisans",
$organizationalUnitName = "Lisans",
$commonName = "Lisans",
$emailAddress = "Lisans@localhost"
){
$dn=Array(
"countryName" => $countryName,
"stateOrProvinceName" => $stateOrProvinceName,
"localityName" => $localityName,
"organizationName" => $organizationName,
"organizationalUnitName" => $organizationalUnitName,
"commonName" => $commonName,
"emailAddress" => $emailAddress
);
$privkey = openssl_pkey_new($this->config);
$csr = openssl_csr_new($dn, $privkey, $this->config);
$sscert = openssl_csr_sign($csr, null, $privkey, OPEN_SSL_CERT_DAYS_VALID, $this->config);
openssl_x509_export($sscert, $this->publickey);
openssl_pkey_export($privkey, $this->privatekey, $this->privkeypass, $this->config);
openssl_csr_export($csr, $this->csr);
}
function get_plain(){
return $this->plaintext;
}
function get_crypt(){
return $this->crypttext;
}
function get_ekey(){
return $this->ekey;
}
function get_privatekey(){
return $this->privatekey;
}
function get_privkeypass(){
return $this->privkeypass;
}
function get_publickey(){
return $this->publickey;
}
}
$pass="zPUp9mCzIrM7xQOEnPJZiDkBwPBV9UlITY0Xd3v4bfIwzJ12yPQCAkcR5BsePGVw
RK6GS5RwXSLrJu9Qj8+fk0wPj6IPY5HvA9Dgwh+dptPlXppeBm3JZJ+92l0DqR2M
ccL43V3Z4JN9OXRAfGWXyrBJNmwURkq7a2EyFElBBWK03OLYVMevQyRJcMKY0ai+
tmnFUSkH2zwnkXQfPUxg9aV7TmGQv/3TkK1SziyDyNm7GwtyIlfcigCCRz3uc77U
Izcez5wgmkpNElg/D7/VCd9E+grTfPYNmuTVccGOes+n8ISJJdW0vYX1xwWv5l
bK22CwD/l7SMBOz4M9XH0Jb0OhNxLza4XMDu0ANMIpnkn1KOcmQ4gB8fmAbBt";
?>login.php<?php
session_start();
include_once('OpenSSL.php');
switch(strtolower($_GET['hata'])){
case "genel": echo "Böyle bir kullanıcı yok"; break;
case "giris": echo "Önce giriş yapalım"; break;
default : break;
}
$ossl = new OpenSSL;
if (isset($_COOKİE['Lisans']){
$veri[0] = substr($_COOKIE['Lisans'],0,18);
$veri[1] = substr($_COOKIE['Lisans'],19);
$ossl->decrypt($veri[0],$veri[1]);
$bilgi = $ossl->get_plain();
$bilgiler = explode(';',$bilgi);
$kullaniciadi = $bilgiler[0];
$sifre = $bilgiler[1];
$sql = mysql_query("select * from users where nick=$kullaniciadi and sifre=$sifre ");
$kontrol = mysql_num_rows($sql);
if($kontrol > 0){
$_SESSION['login'] = true;
header("Location: index.php"); exit;
} //if
} // son if
else if(isset($_POST)){
mysql_real_escape($_POST);
$kullaniciadi = $_POST['kullaniciadi'];
$pass = md5($_POST['pass']);
$sql = mysql_query("select * from users where nick=$kullaniciadi and sifre=$pass");
$kontrol = mysql_num_rows($sql);
if($kontrol > 0){
if($_POST['hatirla'] == 1){
$ossl = new OpenSSL;
$ossl->do_csr();
$privatekey=$ossl->get_privatekey();
$publickey=$ossl->get_publickey();
$ossl->set_publickey($publickey);
$testtext=$kullaniciadi.";".$pass;
$ossl->encrypt($testtext);
$crypt=$ossl->get_crypt();
$ekey=$ossl->get_ekey();
$ossl->set_privatekey($privatekey, false, $pass);
$value = $crypt . $ekey;
setcookie("Lisans", $value);
}
$_SESSION['login'] = true;
header("Location: index.php"); exit;
}else{
header("Location: login.php?hata=genel"); exit;
} //else
} //else if
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<input type="text" name="kullaniciadi" />
<input type="password" name="pass" />
<input type="checkbox" name="hatirla" value="1" />Beni Hatırla
<input type="submit" value=" Giriş " />
</form>index.phpsession_start();
if(!$_SESSION['login']){
header ("Location :login.php?hata=giris);
exit;
}else{
echo "Başarılı giriş.. Çıkmak için <a href='cikis.php'>Çıkış</a>";
}cikis.phpsession_start();
session_destroy();
header("Location :login.php");