Codeigniter https page on site - php

I have built a bespoke site in Codeigniter and my client is getting orders through PayPal.
The problem that is happening is that I have written the site so that if a PayPal order is complete and returns back to the site, The booking gets updated to paid.
I have noticed that some orders, The customer is not returning to the site due sometimes the "Insecure Data" popup you get in most browsers.
I think, The way to resolve this is to make the return page an https:// page.
So am wondering how do you change the config in your CI to the https:// link on just that one page.
There may be other ways and am open to suggestions.
I am also considering the IPN route to update the order, But not too sure on that one.
Thanks

Use MY_url_helper helper.
Add the following code to helper and use it whereever you want to force ssl.
<?php
function ssl_support() {
$CI = & get_instance();
return $CI->config->item('ssl_support');
}
if (!function_exists('force_ssl')) {
function force_ssl() {
if (ssl_support() && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off')) {
$CI = & get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
redirect($CI->uri->uri_string());
}
}
}
if (!function_exists('remove_ssl')) {
function remove_ssl() {
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$CI = & get_instance();
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
redirect($CI->uri->uri_string());
}
}
}
}

I would say put everything in one folder and use SSL for everhting?
application/config/config.php, set base url:
$config['base_url'] = "https://www.yoursite.com/";

Related

Prestashop Quick Address Links to external URL

I have a small problem on the prestashop on adding custom url into the quick address.
The current status of prestashop is 1.7.4.2 fresh install.
As stated from the image above, I would like to redirect it to external URL http://www.google.com, after done creating it is shown in the quick address menu as shown below:
But when I clicked it, just redirect to:
http://localhost:8080/prestashop_1.7.4.2/admin067c8ousl/index.php/http://www.google.com
Note I have deleted the token as it provided the same result
In other words the token is self generated and differs everytime
I have saw original documentation for that specific issue in here.
When you see on the very bottom, it shows the exact issue I am facing:
Note that you can create links to other websites, for instance your PayPal account or your webmail. Simply paste the complete URL in the "URL" field, including the http:// prefix.
As I have written correct url, but it still thinks it is a controller.
I have no modified any code yet, is there a way to fix it.
Thank You and Have a nice day.
That was for v1.6, v1.7 doesn't allow external urls by default. I submitted an improvement for this, hope they approve the merge. Meanwhile, if you want to use them you can modify the classes/QuickAccess.php or add to the override (better option) and change the function getQuickAccessesWithToken to the following:
public static function getQuickAccessesWithToken($idLang, $idEmployee)
{
$quickAccess = self::getQuickAccesses($idLang);
if (empty($quickAccess)) {
return false;
}
$baselink = Context::getContext()->link->getBaseLink();
foreach ($quickAccess as $index => $quick) {
if(strpos($quickAccess[$index]['link'], 'http') !== 0 or strpos($quickAccess[$index]['link'], $baselink) === 0){
if ('../' === $quick['link'] && Shop::getContext() == Shop::CONTEXT_SHOP) {
$url = Context::getContext()->shop->getBaseURL();
if (!$url) {
unset($quickAccess[$index]);
continue;
}
$quickAccess[$index]['link'] = $url;
} else{
// first, clean url to have a real quickLink
$quick['link'] = Context::getContext()->link->getQuickLink($quick['link']);
$tokenString = $idEmployee;
preg_match('/controller=(.+)(&.+)?$/', $quick['link'], $admin_tab);
if (isset($admin_tab[1])) {
if (strpos($admin_tab[1], '&')) {
$admin_tab[1] = substr($admin_tab[1], 0, strpos($admin_tab[1], '&'));
}
$quick_access[$index]['target'] = $admin_tab[1];
$tokenString = $admin_tab[1].(int)Tab::getIdFromClassName($admin_tab[1]).$idEmployee;
}
$quickAccess[$index]['link'] = $baselink.basename(_PS_ADMIN_DIR_).'/'.$quick['link'];
if (false === strpos($quickAccess[$index]['link'], 'token')) {
$separator = strpos($quickAccess[$index]['link'], '?') ? '&' : '?';
$quickAccess[$index]['link'] .= $separator.'token='.Tools::getAdminToken($tokenString);
}
}
}
}
return $quickAccess;
}
Override is not a clean solution.
You can use free module to adding jquery to your "admin header hook" and do it by jquery to change URL of new created quickAccess

Redirect to the same page after switching the language in Codeigniter

I have language switcher on my website, it works fine. However, it redirects to base URL.
But when I write redirect($_SERVER["HTTP_REFERER"]); it doesn't redirect correctly. When I change the new language on the home page, I should stay at the same URL and just make the website change the language.
How do I solve this problem?
Here is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class LanguageSwitcher extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
function switchLang($language = "") {
$language = ($language != "") ? $language : "azerbaijani";
$this->session->set_userdata('site_lang', $language);
redirect(base_url());
}
}
Also, I have tried this, it did not work for me:
function switchLang($language = "") {
if($this->uri->uri_string() == '') {
$language = ($language != "") ? $language : "azerbaijani";
$this->session->set_userdata('site_lang', $language);
redirect(base_url());
} else {
$language = ($language != "") ? $language : "azerbaijani";
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER["HTTP_REFERER"]);
}
}
Try this
$url = $_SERVER['HTTP_REFERER'];
redirect($url);
You could do this with sessions, but I prefer this approach which makes use of an uri-query: What it does, is adding a ?my-current-url part to the link where you call the new language.
So, in my view, I've a link (button, etc.) which calls the language switcher, adding the current site url as uri-query like this:
<?php
// set pathname from where we came from
$pn=uri_string(); // the uri class is initialized automatically
?>
Azerbaijani
then in your function switchLang() you take care of the redirect:
function switchLang($language = "") {
// get pathname from where we came from
$pn= parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$language = ($language != "") ? $language : "azerbaijani";
$this->session->set_userdata('site_lang', $language);
// redirect to where we came from
redirect($pn, 'refresh');
}
Try using :
redirect($uri, 'refresh');
where $uri is the url of the home page that you want to translate
Hope this might help you
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'your domain';
Try to change the base_url in your config.php inside application/config directory with your URL address complete.
What I do in all my projects (maybe not an easy solution for what you're asking for, but realy useful for this and other scenarios) is to use two helpers, I called them history() and back().
History saves current url in an array in session:
$history[] = current_url();
$ci->session->set_userdata('history', $history);
Back($num) receives a number and redirect to that position.
i.e: back(1) is equivalent to "send me to the last visited page".
$history = $this->session->userdata('history');
$cant = count($history);
$back = $cant - $num;
redirect($history[$back]);
Then in the __construct() of "CI_Controller" (or whatever class you extend your controllers) call history() once to log every method.
Also, you can add a third controller, I call it no_history().
no_history() deletes the last saved URL (I use it in methods that I don't want to save, for example, ajax requests). You can also check $this->input->is_ajax_request() but it's just an idea.
That's it, now you have a history of visited URLs.
You can change whatever you want, and then back(1).
Also useful for redirect your users to whatever they were doing before login.
Oh! Almost forgot: Remember to limit the size of the array and always delete the oldest url before adding new one. Between five and ten will be fine.
First load under constructor method like as:
function __construct()
{
parent::__construct();
$this->load->library('user_agent');
}
After inserting or updating data then it will work fine.
redirect($this->agent->referrer());

SSL for selected controller and selected function name [CodeIgniter]

I am here with a serious issue of my site. I am not a php developer but I know the basics. So if I make any mistake here, please try to forgive me.
My site is built with CodeIgnitor and I have SSL for some selected pages which is controlled by controller or controller name or function name.
Like - I am having SSL for these
$ssl_controllers = array('controller1','controller2','controller3');
URL structure is like this - https://mysite.com/controller1
Now, I have several functions inside each controller. Like this - inside of "controller1", I have "function_1()"
So the url for will be - https://mysite/controller1/function_1
Now I dont need SSL for "funtion_1" which is written inside of "controller1"
If I need to remove the SSL for "function_1", I need to remove whole "controller1" from the array. But I don't need that as there is other functions that require SSL.
So how I can remove SSL for "function_1"?
Is there a way to remove a function name from the array?
like this - $ssl_controllers =
$ssl_controllers = array('controller1'==>'function_1','controller2','controller3');
Please help me!!
Thanks!
As I am not using htaccess for redirection, your method is not working in my case. If I use htaccess, it gives me endless loop that cannot be completed!! This is because I am using CodeIgnitor library for redirection and specific page SSL
Here is the PHP code -
class CI_ssl{
function check_ssl()
{
$CI =& get_instance();
$class = $CI->router->fetch_class();
$ssl_controllers = array('controller1','controller2','controller3');
if(in_array($class,$ssl_controllers))
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443)
redirect($CI->uri->uri_string());
}
else
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443)
redirect($CI->uri->uri_string());
}
}
This gives me SSL for specific pages and redirects other to only http://
Use rewrite rules in your .htaccess:
RewriteCond %{SERVER_PORT} !80
RewriteCond %{REQUEST_URI} controller1/function_1
RewriteRule ^(.*)$ http://www.yourdomain.com/controller1/function_1 [R=301,L]
Note: Your base_url must be set to "/" in your config file.
Well after a research today, I finally have resolved my issue. Here is the demo -
$CI =& get_instance();
$class = $CI->router->fetch_class();
$ssl_controllers = array('controller1','controller2','controller3');
In this code, I have called the specific function this way -
$CI =& get_instance();
$class = $CI->router->fetch_class();
$ssl_controllers = array('controller1','controller2','controller3');
$my_function = $CI->router->fetch_method(); // fetch method is used for getting the specific function name
$ssl_functions = array('function_1'); // specific function name in array
Then, I just added elseif statement to my condition -
if(in_array($my_function,$ssl_functions))
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443)
redirect($CI->uri->uri_string());
}
elseif(in_array($class,$ssl_controllers))
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
//echo $CI->config->config['base_url']."=>".$CI->uri->uri_string()."=>".$_SERVER['SERVER_PORT'];exit;
if ($_SERVER['SERVER_PORT'] != 443)
redirect($CI->uri->uri_string());
}
else
{
$CI =& get_instance();
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443)
redirect($CI->uri->uri_string());
}
}
Thus my new url comes without SSL! like this -
http://mysite/controller1/function_1

is_home() function in Codeigniter

As I know the Wordpress has is_home() function to determine home page.
In YII i use solution like this Yii check if homepage
In CI, templates actually, i faced many times with necessity of it. For example, adding some css classes in tag <body>.
All what i found http://ellislab.com/forums/viewthread/194637/#916899
Can anybody help me or write own solution?
Thank in advance
I just wanted to add my answer here as the other method doesn't work with my heavily modified version of CI.
This snippet is what I use to detect if we are on the homepage
if (!$this->uri->segment(1)) {
// We are on the homepage
}
You can use $this->router->fetch_class() to get the current controller and $this->router->fetch_method() to get the method too if needed.
So similar to the Yii example you linked to, you could do something like
$is_home = $this->router->fetch_class() === 'name_of_home_controller' ? true : false;
Or to match method too
$is_home = ($this->router->fetch_class() === 'name_of_home_controller' && $this->router->fetch_method() === 'name_of_home_method') ? true : false;
This way even if the page url is http://yoursite.com/ (assuming the home controller+method is the default), http://yoursite.com/home_controller/, http://yoursite.com/something/that/routes/to/home/controller/, etc, as long as it's calling that controller, it'll set $is_home to true.
Edit: Also you can use ($this->router->fetch_class() === $this->router->default_controller) if you don't want to explicitly state the home controller and it's set to the default controller.
Update for CI v3:
$this->router->fetch_class() and $this->router->fetch_method() were deprecated in CI v3. Use $this->router->class and $this->router->method instead.
if($this->uri->uri_string() == ''){
echo"You are on homepage";
}
If you don't have a custom helper create one; otherwise inside any of your custom helper file just paste any of the following code snippets and you should be able to use is_home() from any location in Codeigniter.
function is_home()
{
$CI =& get_instance();
return (!$CI->uri->segment(1))? TRUE: FALSE;
}
OR
function is_home()
{
$CI =& get_instance();
return (strtolower($CI->router->fetch_class()) === $CI->router->default_controller && $CI->router->fetch_method() === 'index') ? true : false;
}
For better answer you should cover 2 routes for home page:
I assumes there is default controller like this in routes.php:
$route['default_controller'] = 'home';
Your home page without default controller name(like:
yoursite.com)
Your home page with controller name(like:
yoursite.com/home)
if(!$this->uri->segment(1) || $this->uri->segment(1)=='home'){
// YOU ARE ON THE HOME
}
Why not just simply
public function name_of_home_method()
{
$data['is_home'] = true;
}

https request on some pages but not on all pages zend framework

I need to put https on some of the URL but not on all the URL. I am using zend URl view helper for all the links. I have a *.example.com SSL certificate for whole site. Now I open site with https://www.example.co, then all the link on home page or other pages contains https in URL. How can I make some specific request on the https url and other page should be opened normal.
I also need to pt some redirection so that if somebody open the specific pages in normal URL then they redirected on https url. I think the .htaccess redirection will work for that.
Any help ????
Thanks in advance!!!
the URL ViewHelper only assembles paths, absolute from the hostname. so you need to explicitly prefix your https links
<? $url = $view->url(array('some' => 'params') /*, $route, $reset*/) ?>
my explicit https link
you should maybe create a an own small viewhelper which does that work for your and also checks if HTTP_HOST is set etc, maybe also take it from the config instead from $_SERVER.
$view->httpsUrl(array('some' => 'params')/, $route, $reset/);
to be secure that defined reqeust must be https can easily be done by adding a front controller plugin or even an abstract controller-class you base all your otehr controller on.
a plugin could look like this
My_Controller_Plugin_HttpBlacklist extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
// when /foo/bar/baz is requested
if (($request->getModuleName() == 'foo' &&
$request->getControllerName() == 'bar' &&
$request->getControllerName() == 'baz')
/* || (conditions for more requests)*/) {
//very basic confifiotn to see if https is enabled, should be done better...
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
// should be done with Zend_Http_Reponse instead
header('Location: https://'. $_SERVER['HTTP_HOST] . $_SERVER['REQUEST_URI']);
exit;
}
}
}
}
then simply plug it in
$frontController->registerPlugin(new My_Controller_Plugin_HttpBlacklist);
I rather used a simple method and that does not required any change in urls generation/route. I wrote following lines in routeStartup function of a plugin and haven't made any change in URLs route.
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$controller=$this->_controller = $this->_front->getRequest()->getControllerName();
$action=$this->_controller = $this->_front->getRequest()->getActionName();
if($_SERVER['SERVER_PORT']!=443)
{
//controller and actions array to check ssl links
$actionArr=array('index'=>array('registration'),'account'=>array('editacc','edit'),'dealer'=>array('*'));
if(array_key_exists($controller,$actionArr)!==false)
{
if(in_array($action,$actionArr[$controller]) || $actionArr[$controller][0]=='*')
{
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl(SITE_LIVE_URL_SSL.$controller."/".$action);
}
}
}
else
{
//controller and action array that should not be on ssl.
$notAactionArr=array('usersearch'=>array('mainserarch'),'account'=>array('index'),'onlineusers'=>array('*'));
if(array_key_exists($controller,$notAactionArr)!==false)
{
if(in_array($action,$notAactionArr[$controller]) || $notAactionArr[$controller][0]=='*')
{
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl(SITE_LIVE_URL.$controller."/".$action);
}
}
}
}

Categories