我想在我的网站中使用会话,但我无法找到存储它们的最佳方式, codeigniter 提供文件,< em>数据库等。但哪个最好??
我使用此配置文件:
<?php
$dbname = $_COOKIE['database'];
backup_tables('localhost','user','password',$dbname);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$dbname,$tables = '*')
{
$return='';
$link = mysql_connect($host,$user,$pass);
mysql_select_db($dbname,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('backup-'.$dbname.'-'.date('d-m-y').'.sql','w+');
$backup = 'backup-'.$dbname.'-'.date('d-m-y').'.sql';
fwrite($handle,$return);
fclose($handle);
function uploadfile($file)
{
$ftp_server = "ftp.domain.com";
$ftp_user_name = "user@domain.com";
$ftp_user_pass = "ftp_password";
// open some file for reading
//$file = $bname;
$fp = fopen($file, 'r');
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Backup Successfully Uploaded \n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
}
uploadfile($backup);
$link ='http://domain.com/update.php?bname='.$backup.'&dbname='.$dbname;
//echo "<br><a href='http://domain.com/update.php?bname=$backup&dbname=$dbname' target='_blank'> Click Here </a> to Update Database online";
echo("<div class='alert alert-success alert-dismissable'>
<button type='button' class='close' data-dismiss='aler' aria-hidden='true'>×</button>
<i class='fa fa-inbox fa-2x'> </i> <a href='$link' target='new' class='alert-link'>Click Here </a>to Update Database Online.
</div> ");
}
?>
</div>
</div>
但是我收到了这个错误:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = '_s';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 0;
$config['sess_regenerate_destroy'] = FALSE;
我必须使用数据库 ??
答案 0 :(得分:4)
我发现最好的方法是在系统目录中创建一个缓存/会话/文件夹更安全我把日志和缓存等重要内容放在系统而不是应用程序文件夹中。
http://www.codeigniter.com/user_guide/libraries/sessions.html
的config.php
$config['encryption_key'] = 'somekey';
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = BASEPATH . 'cache/sessions/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;
我也会自动加载会话
<强>应用/配置/ autoload.php 强>
$autoload['libraries'] = array('database', 'session');
用法示例
登录form_validation成功部分
$data = array(
'is_logged' => true,
'username' => $this->input->post('username')
);
$this->session->set_userdata($data);
您设置数据然后登录后可以获得会话$this->session->userdata('username')
等