creating high secured encrypted id to website url in codeigniter? - codeigniter

Controller:
function verifyuser()
{
$uri = $this->uri->uri_to_assoc(4);
//print_r($uri);
if( isset( $uri['id'] ) && $uri['id'] != '' )
{
$where = array();
$where['roll_number'] = $uri['id'];
$this->data['verification'] = $this->admin_model->verify($where);
}
else
{
$this->data['verification'] = array();
}
$this->data['content'] = 'admin/verification';
$this->view($this->data);
}
MODEL:
function verify($where='')
{
$this->db->select('*');
$this->db->from( $this->db->dbprefix('members') );
if( !(empty( $where )) )
$this->db->where( $where );
$result = $this->db->get();
//echo $this->db->last_query();
return $result->result();
}
www.example.com/user/admin/verify/345
to
www.example.com/user/admin/verify/hgf_877%%%_oi
like this.

you can use any of encoding's defined in PHP.net Manual for your security purposes. But i prefer base64_encode and base64_decode. Which i am using my application for encoding comples strings.
<?php
$temp = 345;
$res = base64_encode($temp);
echo $res;echo "<br>";
$result = base64_decode($res);
echo $result;
?>
You can find small example in this link also.

Related

Codeigniter3.x calling multiple stored procedure cache

This is somewhat related to [CodeIgniter active records' problems calling multiple stored procedures
but I am not experiencing a blank page; instead when I am passing my data array to view it seems that the the preceding array also being drag to the view.
model
public function data1($student) {
$year = 1;
$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql, array($course, $student, $year, $sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
public function data2($student) {
$year = 1;
$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
} else {
mysqli_next_result( $this->db->conn_id );
return $query->result();
}
}
Controller:
$data['data1']=data1 from my model(SP);
$data['data2']=data2 from my model(SP);
View:
foreach($data2 as key => $value ) {
echo ....;
}
Here's the PROBLEM... in the view, I only wanted to output the $data2 but to my surprise $data1 is also being output.
Does anybody else have this issue?
I JUST SOLVE IT.
Model
public function data1($student){
**$this->db->initialize();**
$year = 1;$sem = 1;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
public function data2($student){
**$this->db->initialize();**
$year = 1;$sem = 2;
$course = $this->getStudentCourseByStudentId($student);
$sql = "CALL EVALUATION_BY_YEAR_SEM(?,?,?,?)";
$query = $this->db->query($sql,array($course,$student,$year,$sem));
if (!$query) {
return $this->db->error();
}else {
mysqli_next_result( $this->db->conn_id );
return $query->result();**$this->db->close();**
}
}
controller
$data['data1']=data1 from my model(SP);
**$this->db->close();**
$data['data2']=data2 from my model(SP);

$this->pagination->create_links(); returning nothing

Link for pagination not working
This is my controller.
Here Is my code. What's wrong in this.
Here links are not Initialize. Tell me the correct solutions
The create_links(); the function seems to not be working. I don't get any errors, but it just returns a blank string. I'm aware the documentation says http://codeigniter.com/user_guide/libraries/pagination.html The create_links() function returns an empty string when there is no pagination to show. but so how do I fix that? Thanks you!
public function Index()
{
$this->load->model('Testmodel');
$conf = array();
$conf['base_url'] = base_url()."index.php/user/view";
$conf['total_rows'] = $this->db->get('user')->num_rows();
$conf['per_page'] = 5;
$conf['use_page_numbers'] = TRUE;
$conf['num_links'] = 2;
$conf['cur_tag_open'] = ' <a class="current">';
$conf['cur_tag_close'] = '</a>';
$conf['next_link'] = 'Next';
$conf['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 0;
}
$data['records'] = $this->Testmodel->fetch_data($conf['per_page'], $page);
$this->pagination->initialize($conf);
$data['links'] = $this->pagination->create_links();
}
Model Code:
public function fetch_data($limit, $start) {
$this->db->select('user.*,country.countryName,state.stateName,city.cityName');
$this->db->from('user');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;

DataBase Schema File (.sql file ) Download with laravel

Schenario :
1) suppose that i have a database called myDataBase
2) Assume that myDataBase have some tables like A,B,C,D
3) i have to download the schema of the table A,B from the database with name myDataBase
Hope you have done the migration of all the table of your project . If you are using a xampp server then goto http://localhost/phpmyadmin and select your database and then goto export tab on the top tab bar and simply export with go button
<?php
//ENTER THE RELEVANT INFO BELOW
$mysqlUserName = "root";
$mysqlPassword = "";
$mysqlHostName = "localhost";
$DbName = "ukkoteknik";
$backup_name = "mybackup.sql";
$tables = array("admin", "sample");
Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName, $tables, $backup_name=false );
function Export_Database($host,$user,$pass,$name, $tables=false, $backup_name=false )
{
$mysqli = new mysqli($host,$user,$pass,$name);
$mysqli->select_db($name);
$mysqli->query("SET NAMES 'utf8'");
$queryTables = $mysqli->query('SHOW TABLES');
while($row = $queryTables->fetch_row())
{
$target_tables[] = $row[0];
}
if($tables !== false)
{
$target_tables = array_intersect( $target_tables, $tables);
}
foreach($target_tables as $table)
{
$result = $mysqli->query('SELECT * FROM '.$table);
$fields_amount = $result->field_count;
$rows_num=$mysqli->affected_rows;
$res = $mysqli->query('SHOW CREATE TABLE '.$table);
$TableMLine = $res->fetch_row();
$content = (!isset($content) ? '' : $content) . "\n\n".$TableMLine[1].";\n\n";
for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0)
{
while($row = $result->fetch_row())
{ //when started (and every after 100 command cycle):
if ($st_counter%100 == 0 || $st_counter == 0 )
{
$content .= "\nINSERT INTO ".$table." VALUES";
}
$content .= "\n(";
for($j=0; $j<$fields_amount; $j++)
{
$row[$j] = str_replace("\n","\\n", addslashes($row[$j]) );
if (isset($row[$j]))
{
$content .= '"'.$row[$j].'"' ;
}
else
{
$content .= '""';
}
if ($j<($fields_amount-1))
{
$content.= ',';
}
}
$content .=")";
//every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num)
{
$content .= ";";
}
else
{
$content .= ",";
}
$st_counter=$st_counter+1;
}
} $content .="\n\n\n";
}
//$backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
$date = date("Y-m-d");
$backup_name = $backup_name ? $backup_name : $name.".$date.sql";
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$backup_name."\"");
//echo $content; exit;
}
?>

How to pass a message from model to controller codeigniter?

I want to pass a message variable in some conditions to controller from model in codeigniter. But when i am doing this it is printing only "No" everytime.
Model is
public function add_city() {
/* Storing form data into an array */
$data = array(
'city_name' => $this->input->post('city'),
'city_overview' => $this->input->post('overview')
);
/* Checking if already exist in database */
$query = $this->db->query("SELECT * FROM city_tbl WHERE city_name='" . $data['city_name'] . "' ORDER BY id ASC");
$count_row = $query->num_rows();
if ($count_row > 0) {
$msg = "No";
} else {
$this->db->insert('city_tbl', $data);
$msg = "Yes";
}
return $msg;
}
And Controller is
public function addingCity() {
$this->add_model->add_city();
var_dump($msg);
//redirect("/city");
}
Try This also
public function add_city() {
$data = array(
'city_name' => $this->input->post('city'),
'city_overview' => $this->input->post('overview')
);
/* Checking if already exist in database */
$query = $this->db->query("SELECT * FROM city_tbl WHERE city_name='" . $data['city_name'] . "' ORDER BY id ASC");
$count_row = $query->num_rows();
if ($count_row > 0) {
$msg = "No";
} else {
$this->db->insert('city_tbl', $data);
$msg = "Yes";
}
return $msg;
}
On controller redirect message with get method -
public function addingCity() {
$msg = $this->add_model->add_city();
redirect("/city?msg=".$msg);
}
And finally print this message on view page
echo $this->input->get('msg');
You need to assign value to $msg in your controller
public function addingCity() {
$msg=$this->add_model->add_city();// assign
var_dump($msg);
//redirect("/city");
}
Try this
public function add_city() {
$city_name = $this->input->post('city');
$city_overview = $this->input->post('overview');
$query = $this->db->query("SELECT * FROM city_tbl WHERE city_name='$city_name' ORDER BY id ASC");
$result = $query->result_array()
$count = count($result);
if(empty($count))
{
$msg = "No";
return $msg;
}
else{
$data = array(
'city_name'=>$this->input->post('city'),
'city_overview'=>$this->input->post('overview') );
$this->db->insert('city_tbl', $data);
$msg = "Yes";
return $msg;
}
}
In Controller
public function addingCity() {
$msg = $this->add_model->add_city();
echo $msg;
}
To pass data to view
public function addingCity() {
$data['msg'] = $this->add_model->add_city();
$this->load->view("filename", $data); # ex $this->load->view("index", $data);
}
Error is in your controller function
Try with this code
public function addingCity() {
$msg = $this->add_model->add_city();
echo $msg;die;
//redirect("/city");
}

How to write Some Data in CSV file in Magento?

I am trying print some shipment details as a CSV file and I am almost done in implementing this but it does not gives the the option for downloading.It automatically create the file and save it in the directory mentioned.
I think I am missing _prepareDownloadResponse() function but how do I implement this.
I am using this code ->
$file_path = 'sample_shipment.csv';
$mage_csv = new Varien_File_Csv();
$mage_csv->saveData($file_path, $content);
Edit:
All this code is in my indexcontroller.php
full code in indexcontroller.php ->
class Company_Manifestupload_IndexController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {
echo 'Manifestupload Index!';
}
public function bluedartmanifestuploadAction() {
$request = $this->getRequest();
$shipmentIds = $request->getPost('shipment_ids', array());
$file_path = 'manifest_upload_'.Mage::getSingleton('core/date')->date('d-m-Y_H-i-s').'_csv.csv';
$mage_csv = new Varien_File_Csv();
$shipment_csv=array();
/* Putting data in $shipment_csv[] */
$mage_csv->saveData($file_path, $shipment_csv);
}
In order to make the csv file downloadable, just put the following code inside the related controller action:
$fileName = 'filename.csv';
$content = $csvData; //prepare the csv data and return as string
$this->_prepareDownloadResponse($fileName, $content); //this will make the csv file downloadable with the $content as contents in it.
Hope this helps.
Edit:
Please find the updated controller action code.
public function bluedartmanifestuploadAction() {
$request = $this->getRequest();
$shipmentIds = $request->getPost('shipment_ids', array());
$fileName = 'manifest_upload_'.Mage::getSingleton('core/date')->date('d-m-Y_H-i-s').'_csv.csv';
//prepare csv contents
#prepare header
$csv = '';
$_columns = array(
"field1",
"field2",
//...
);
$data = array();
foreach ($_columns as $column) {
$data[] = '"'.$column.'"';
}
$csv .= implode(',', $data)."\n";
#prepare data
foreach($dataFromShipmentIds as $_data){ //just dummy loop
$data = array();
$data[] = $_data['field1'];
$data[] = $_data['field2'];
//...
$csv .= implode(',', $data)."\n";
}
//now $csv varaible has csv data as string
$this->_prepareDownloadResponse($fileName, $csv);
}
Edit 2: The above code will only work properly in ideal case.It will not generate proper Csv when there "\n"s and ","s in the fields .To make it work properly one can use code from varien file CSV .I'll add it here-
First make array of values in $data[] as above then instead of using $csv.=implode(',',$data)."\n" use the below code.
foreach ($data as $value) {
if (strpos($value, $delimiter) != false ||
strpos($value, $enclosure) != false ||
strpos($value, "\n") != false ||
strpos($value, "\r") != false ||
strpos($value, "\t") != false ||
strpos($value, ' ') != false) {
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++) {
if ($value[$i] == $escape_char) {
$escaped = 1;
} else if (!$escaped && $value[$i] == $enclosure) {
$str2 .= $enclosure;
} else {
$escaped = 0;
}
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
} else {
$str .= $enclosure.$value.$enclosure.$delimiter;
}
}
$str = substr($str,0,-1);
$str .= "\n";
If you want to create downloadable csv of you ordered item's you can use following script.
$orders = Mage::getModel("sales/order")->getCollection();
// prepare CSV header
$csv = '';
$_columns = array(
"Order Id",
"Product Name",
"Sku",
"Price"
);
$data = array();
// prepare CSV header...
foreach ($_columns as $column) {
$data[] = '"'.$column.'"';
}
$csv .= implode(',', $data)."\n";
foreach ($orders as $order) {
$items = $order->getAllItems();
foreach ($items as $item) {
$loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
//prepare csv contents
$data = array();
$data[] = $order->getId();
$data[] = $loadProduct['name'];
$data[] = $loadProduct['sku'];
$data[] = $loadProduct['price'];
$csv .= implode(',', $data)."\n";
//now $csv varaible has csv data as string
}
}
$this->_redirect('*/*/');
$this->_prepareDownloadResponse('file.csv', $csv, 'text/csv');
For more info visit here

Resources