Can I use codeigniter redirects function contains variable name like this?
redirect($page_url);
I have page url in a session variable so, im saving session in codeigniter varible
$page_url=$_SERVER['REQUEST_URI'];
$page_url=explode("/", $page_url, 3);
$data = array('page_url'=>$page_url[2],'validated' => true);
$this->session->set_userdata($data);
Yes you can use variables surely but make sure you are loading URL helper first and checking if the $page_url variable is set. For example:
$this->load->helper('url');
if(isset($page_url) && $page_url != '') {
redirect($page_url);
} else {
redirect('/index.php');
}
Related
I'm trying to pass a varible from the Controller to my .blade.php file.
I'm returning the view and compacted variables to the .blade.php but it doens't recognize the
variable.
This is the code of the Controller.
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison')->with(compact('contents'),$contents)->with(compact('contents2'),$contents2);
And i'm trying every way just to get an result but instead i'm getting the "Undefined variable $contents" page. The last method i used was a simple
<p>{{$contents}}</p>
I don't think it's correct but i don't really remember how to do it.
In controller return like:
return view('comparison.comparison', compact(['contents', 'contents2']);
And make sure your file is in resources/views/comparison/comparison.blade.php
Try this
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison', compact('contents','contents2'));
if you have defined a veriable above just use tha name inside compact as above and it can be acced inside blade as <p>{{$contents}}</p>
You can pass variables like that it's not mandatory to use compact.
return view('comparison.comparison', [
'contents' => $contents,
'contents2' => $contents2
]);
or if you want with compact:
return view('comparison.comparison')->with(compact('contents', 'contents1'));
I am trying to dynamically set database connection credentials based on who logs into a web page. I'm pretty sure it's not working because of the $connectdb variable not being defined. Can someone please check out my code and try to get it working? Thanks!
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$connectdb="";
class Main extends CI_Controller {
function __construct() {
parent::__construct();
echo $connectdb;
$this->load->database($connectdb);
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
public function index() {
if ($_POST["username"] == "root") {
global $connectdb="default";
}
if ($_POST["username"] == "user1") {
global $connectdb="user1";
}
if ($_POST["username"] == "user2") {
global $connectdb="user2";
}
$connect = #mysql_connect("localhost", $_POST["username"], $_POST["password"]);//won't display the warning if any.
if (!$connect) {
echo 'Server error. Please try again sometime. CON';
} else {
print("Employees");
echo "<br>";
print("Visitors");
}//Just an example to ensure that we get into the function
// LOAD LIBRARIES
}
public function employees() {
$this->grocery_crud->set_table('employees');
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
public function visitors() {
$this->grocery_crud->set_table('visitors');
$output = $this->grocery_crud->render();
$this->_example_output($output);
}
function _example_output($output = null) {
$this->load->view('our_template.php',$output);
}
}
A quick read of THE MANUAL will show you it's pretty easy to have multiple database connections. You define the connection parameters in your database.php config file then call the database with the group name.
if($user == 'someguise'){
$this->load->database('guiseDB');
}
HTH
For something as important as this i would strongly suggest running the form input through CI form validation first. You really should be validating and doing things like limit the number of characters, make sure its letters only, trim whitespace and XSS cleaning - all before you do anything else. (this helps your user as well)
then to get the value from the form - do something like this
$username = $this->input->post( 'username', TRUE ) ;
and work with the one variable $username. the TRUE XSS cleans the value, and then instead of repeating
$_POST["username"] ==
over and over and over, you are just checking $username. also makes the code easier to read. if you need $username in different methods just use:
$this->username = $this->input->post( 'username', TRUE ) ;
and then $this->username will work in any method in the class.
finally consider having a table of users or config list -- and then use a different value to call your database. in other words maybe they log in with the user name: "root" but then its a different name like global $connectdb = "rootadmin"
The session value lost after the redirect. I m redirecting from pay_order method to do_payment method. when I print the session value in do_payment method its return false. just simple calling the do_payment method display the session value. Please help what is the problem with redirect..?
function pay_order($order_id)
{
$this->load->helper('url'); //loading url helper
$this->load->library('session');//loading session lib
$this->load->library('cart'); //loading cart lib
$this->load->helper('form');//loading form helper
$output = $this->cart->contents();// getting data from cart.
$output = $this->sort_array($output);// sorting the array
$list['data'] = $output;
$list['order_id'] = $order_id;
$this->session->set_userdata('abc', $list);// setting the session
redirect('checkout/do_payment'); // redirecting to do_payment
}
function do_payment()
{
$this->load->helper('url'); //loading url helper
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
var_dump( $arr );// return false value.
//$this->load->view('order/pay_through_gateway');
}
when I redirect from pay_order method the session is not available in do_payment method. why?
There is a problem, check the line in the method pay_order() where you are setting the session.
$list['order_id'] = $order_id
$this->session->set_userdata('abc', $list);// setting the session
Before that line you have missed semicolon. And everything looks fine.
Update:
There is no error as I think, check whether you may have null value while you are storing the values in session. And check the values by following code in do_payment() method.
function do_payment(){
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
print_r( $arr );// print the $arr content
}
Change this:
$list['order_id'] = $order_id
To this:
$list['order_id'] = $order_id;
You have a semicolon missing. You do not see any errors because of the redirection.
I have a base controller (base) which all other controllers extend from.
Anything placed here will override other controllers, the redirects will be here.
URLs example:
http://domain.com/controllerone/function
http://domain.com/controllertwo/function
http://domain.com/controllerthree/function
Using the code below. will give me the controller name
$this->uri->segment(1);
Each of the above controllers need to be redirected to separate URLs, but the funcation part should not change:
http://domain.com/newcontrollerone/function
http://domain.com/newcontrollertwo/function
http://domain.com/newcontrollerthree/function
In my base controller i want the following logic:
$controller_name = $this->uri->segment(1);
if($controller_name === 'controllerone'){
// replace the controller name with new one and redirect, how ?
}else if($controller_name === 'controllertwo'){
// replace the controller name with new one and redirect, how ?
}else{
// continue as normal
}
i was thinking i should use redirect() function and str_replace(), but dont know how efficient these would be. Ideally i do not want to use the Routing class.
thanks.
try
header("Location:".base_url("newcontroller/".$this->uri->segment(2)));
Simple Solution using segment_array:
$segs = $this->uri->segment_array();
if($segs[1] === 'controllerone'){
$segs[1] = "newcontroller";
redirect($segs);
}else if($segs[1] === 'controllertwo'){
$segs[1] = "newcontroller2";
redirect($segs);
}else{
// continue as normal
}
CodeIgniter's URI Routing, should be able to help in this case. However, if you have a good reason not to use it, then this solution may help.
The potential redirects are in an array, where the key is the controller name being looked for in the URL and the value is the name of the controller to redirect to. This may not be the most efficient but I think it should be easier to manage and read than a potentially very long if-then-else statement.
//Get the controller name from the URL
$controller_name = $this->uri->segment(1);
//Alternative: $controller_name = $this->router->fetch_class();
//List of redirects
$redirects = array(
"controllerone" => "newcontrollerone",
"controllertwo" => "newcontrollertwo",
//...add more redirects here
);
//If a redirect exists for the controller
if (array_key_exists($controller_name, $redirects))
{
//Controller to redirect to
$redirect_controller = $redirects[$controller_name];
//Create string to pass to redirect
$redirect_segments = '/'
. $redirect_controller
. substr($this->uri->uri_string(), strlen($controller_name)); //Function, parameters etc. to append (removes the original controller name)
redirect($redirect_segments, 'refresh');
}
else
{
//Do what you want...
}
I am getting url like http://localhost/webpt/ipn/checkout/?token=EC-2YD51592ET0280122&PayerID=VNH3J2KQEK8AS and want to catach in my controller. in my controller code
function checkout($token = array()) {
echo"<pre>";
print_r($token);
echo"</pre>";
}
but it show empty array.
Ok I just set $config['uri_protocol'] = 'AUTO'; in config.php & use
echo ($_GET['token']); or print_r($this->input->get()); // print all the get values & it work fine thanks to all.
you can grab value of token by $this->input->get('token'); since it is passed in url following a question mark.
if you mean that you want to catch the param token,
you have two options :
format your url to be like
http://localhost/webpt/ipn/checkout/nouman
and you catch it in your controller like this :
function checkout($token) {
echo $token;
}
or use $this->input->get('token')
function checkout() { // http://localhost/webpt/ipn/checkout/?token=8767¶m2=333
echo $this->input->get('token'); // echo the name param
print_r($this->input->get()); // print all the get values
}