In my MY_Controller.php, I want to detect user device and the requested domain name. Domain names I am using on same app are: www.seeme.tld and m.seeme.tld, also I am using $this->detect().
So this is what I did:
<?php
if($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
$this->config->set_item('base_url', MOBILE_URL);
}elseif(!$this->detect->isMobile() || $_SERVER['HTTP_HOST'] != MOBILE_URL){
$this->config->set_item('base_url', WEBSITE_URL);
}
?>
I have 2 folders in application/views folder : PC(for pc users) and Mobile(for mobile users)
In order to load views, I used this code in my fetch() function:
public function fetch($view, $data = array, $other_vars = false)
{
if(base_url() === MOBILE_URL || $this->_ci->detect->isMobile()){
$f = 'Mobile/';
}elseif(!$this->_ci->detect->isMobile() || base_url() != MOBILE_URL){
$f = 'PC/';
}
return $this->_ci->load->view($f.'contents/'.$view, $data, true);
}
When I use a mobile device or the visit m.seeme.tld with a mobile device, I get mobile contents. But when I visit visit m.seeme.tld with a PC instead of getting mobile contents, I rather get PC contents. Please help me solve this issue!
Changing config array could be problematic sometimes: How to override config's array within a controller in CodeIgniter?. Also you are doing double check (in controller and in the function).
I archieve similar behaviour with doing this way:
YOURCONTROLLER.PHP __construct():
if ($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
define('IS_MOBILE', TRUE);
}else{
define('IS_MOBILE', FALSE);
}
then you could use to load the view:
if (IS_MOBILE) {
$view_folder = 'Mobile/';
}else{
$view_folder = 'PC/';
}
$this->load->view($view_folder.$view, $data, TRUE);
Also you could add a single checkpoint to view if the if statment is working fine:
if ($this->detect->isMobile() || $_SERVER['HTTP_HOST'] === MOBILE_URL){
define('IS_MOBILE', TRUE);
log_message('debug', 'Im mobile browser: '.$this->detect->isMobile().' or the url is mobile:'.$_SERVER["HTTP_HOST"]);
}else{
define('IS_MOBILE', FALSE);
log_message('debug', 'Im pc');
}
Hope it helps to you.
// This works only replace the '===' with '='.
thanks for the help but i got it fixed. all i did was to first detect mobile users in MY_Controller.php and redirect them to MOBILE_URL then in my fetch() i did:
if($_SERVER['HTTP_HOST'] = MOBILE_URL){
$view_folder = 'Mobile/;
}else{ $view_folder = 'Frontend/;
}
and that's it,Paam it started working.
Related
I have such a prolem since I had to change the server provider.
I was porting a few wordpress based sites using the Duplicator plugin. Now wherever in the content of the page I use the tag, it returns an error
Warning: strpos(): Empty needle in /XXX/post.php on line XXXX
It's about the code:
function is_local_attachment($url) {
if (strpos($url, home_url()) === false) {
return false;
}
if (strpos($url, home_url('/? attachment_id =')) ! == false) {
return true;
}
$id = url_to_postid($url);
if ($id) {
$post = get_post($id);
if ('attachment' == $post-> post_type) {
return true;
}
}
return false;
}
and more specifically about the line:
if (strpos($url, home_url()) === false) {
Has anyone had such a case and knows how to solve it?
Everything was fine before the server switch. And I wish it would continue to be the case. I'd rather not fix the bug, but find and eliminate what led to it.
The only thing that comes to mind is whether I didn't have Site Address URL on the previous server, I don't have to have it on the new one - the field is empty.
The easiest way to solve this is to put a "guard clause" in. Something like this:
function is_local_attachment($url) {
$homeUrl = home_url();
if (empty($home_url)) {
return;
}
. . .
I don't know what home_url() returns but obviously it can return an empty string and that is causing you a probelm.
If you are using PHP 7.1 or higher you could use something like:
if (strpos($url, home_url()??' ') === false)
Personally, I wouldn't because it's more difficult to read, but it would solve your problem.
Cheers! :)
=C=
Dear friends I have installed prestashop on my existing website.My current website has a login system that I have already built.
Because of installing prestashop for my system,I thought to change my existing login to prestashop login.
As for the prestashop documentation,to access prestashop cookie outside prestashop,I made a test page to retrieve cookie data as follows,
include_once('path_to_prestashop/config/config.inc.php');
include_once('path_to_prestashop/config/settings.inc.php');
include_once('path_to_prestashop/classes/Cookie.php');
$cookie = new Cookie('ps');
print_r($cookie);
But this is not working and browser says
It contains redirect loop.
I tried to disable SEO friendly url and cannonical url to no-direct as some posts suggested.
Now if I go to the test page it redirects to the prestashop index page rather displaying cookie data.
What should I do to overcome this problem?
Thank you.
When you include config/config.inc.php PrestaShop redirects to the shop domain.
The following code is causing this behavior in classes/shop/Shop.php:
$shop = new Shop($id_shop);
if (!Validate::isLoadedObject($shop) || !$shop->active)
{
// No shop found ... too bad, let's redirect to default shop
$default_shop = new Shop(Configuration::get('PS_SHOP_DEFAULT'));
// Hmm there is something really bad in your Prestashop !
if (!Validate::isLoadedObject($default_shop))
throw new PrestaShopException('Shop not found');
$params = $_GET;
unset($params['id_shop']);
$url = $default_shop->domain;
if (!Configuration::get('PS_REWRITING_SETTINGS'))
$url .= $default_shop->getBaseURI().'index.php?'.http_build_query($params);
else
{
// Catch url with subdomain "www"
if (strpos($url, 'www.') === 0 && 'www.'.$_SERVER['HTTP_HOST'] === $url || $_SERVER['HTTP_HOST'] === 'www.'.$url)
$url .= $_SERVER['REQUEST_URI'];
else
$url .= $default_shop->getBaseURI();
if (count($params))
$url .= '?'.http_build_query($params);
}
$redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
header('HTTP/1.0 '.$redirect_type.' Moved');
header('location: http://'.$url);
exit;
}
You could override the Shop class to disable the redirect for your script.
To do this first define PS_DISABLE_SHOP_REDIRECT constant before you include config/config.inc.php:
define('PS_DISABLE_SHOP_REDIRECT', true);
Then paste the following before the previous code in the overridden class:
if (defined('PS_DISABLE_SHOP_REDIRECT')) {
$id_shop = Configuration::get('PS_SHOP_DEFAULT');
}
I am currently developing a PHP application that is (hopefully) going into production use soon.
What I'm needing help with is detecting what URL the app is being accessed on ie dev.local, testing.domain.com or app.domain.com and then using the correct MySQL DB, ie app_test for dev and testing and app_prod for the production server.
Along with that, I also want to be able to modify the internal URLs to match (several emails are sent that also need to be tested with the correct URL).
I remember seeing some stuff about it before but am not able to find it any more.
Get full url of page
function request_url() {
$result = '';
$default_port = 80;
if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']=='on')) {
$result .= 'https://';
$default_port = 443;
} else {
$result .= 'http://';
}
$result .= $_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != $default_port) {
$result .= ':'.$_SERVER['SERVER_PORT'];
}
$result .= $_SERVER['REQUEST_URI'];
return $result;
}
I think you will be enough: $_SERVER['SERVER_NAME']
Easy way to do that ......
Define environment constants in constants.php file
// constants.php
define('ENVIRONMENT', 'development');
//define('ENVIRONMENT', 'production'); // uncomment this when your going to live your project
define general functions in general.php
// general.php
include "constants.php";
function is_production()
{
if(ENVIRONMENT == "production")
{
return TRUE;
}
return FALSE;
}
function is_development()
{
if(ENVIRONMENT == "development")
{
return TRUE;
}
return FALSE;
}
Now you can us that functions in your database connection files and select your database and base url
// in db.php
include "general.php";
if(is_production())
{
$conn = mysql_connect("host1","username1","password1");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://domain.com');
}
else if(is_development())
{
$conn = mysql_connect("host2","username2","password2");
mysql_select_db("db1",$conn);
define('BASE_URL', 'http://testing.domain.com');
}
Now You can use that BASE_URL constant and you have database connection as you want
This general overview but you can implement in your project as your standered.. :)
Some websites are not allowed to be embedded via iframe. They produce the following error:
Refused to display 'https://news.ycombinator.com/news' in a frame because it
set 'X-Frame-Options' to 'DENY'.
Our app allows URL submissions from users. We want to check on the server side if the website could be embedded in iframe and add a corresponding flag. On the client we check for the flag, and either do iframe embed or just provide a direct link to a webpage.
How do I check whether website will support iframe or not?
Try this code:
$url = "http://www.google.com/";
$url_headers = get_headers($url);
foreach ($url_headers as $key => $value)
{
$x_frame_options_deny = strpos(strtolower($url_headers[$key]), strtolower('X-Frame-Options: DENY'));
$x_frame_options_sameorigin = strpos(strtolower($url_headers[$key]), strtolower('X-Frame-Options: SAMEORIGIN'));
$x_frame_options_allow_from = strpos(strtolower($url_headers[$key]), strtolower('X-Frame-Options: ALLOW-FROM'));
if ($x_frame_options_deny !== false || $x_frame_options_sameorigin !== false || $x_frame_options_allow_from !== false)
{
echo 'url prevent iframe!';
}
}
X-Frame-Options is a response header sent by the server, so have your server perform an HTTP GET on the URL you'd like to test, see if the X-Frame-Options header is present, and if it is... judging by the spec you're not likely to be allowed to embed it at all.
I wrote this function:
function allowEmbed($url) {
$header = #get_headers($url, 1);
// URL okay?
if (!$header || stripos($header[0], '200 ok') === false) return false;
// Check X-Frame-Option
elseif (isset($header['X-Frame-Options']) && (stripos($header['X-Frame-Options'], 'SAMEORIGIN') !== false || stripos($header['X-Frame-Options'], 'deny') !== false)) {
return false;
}
// Everything passed? Return true!
return true;
}
The following code is in the index.php file of my site and runs every time a page on my website is queried. Both of these requires execute there own respective 404 error pages so dont worry about that. What is the most performance efficient way of doing this within php?
$cache = $_SERVER['REQUEST_URI'];
if(preg_match('/^\/blog/',$cache) || preg_match('/^\/portfolio/',$cache)){
define('WP_USE_THEMES', true);
// Loads the WordPress Environment and Template
require('./wordpress/wp-blog-header.php');
}else{
// Load codeigniter
require('./codeigniter/index.php');
}
$cache = $_SERVER['REQUEST_URI'];
if (0 === strpos($cache, '/blog/') ||
0 === strpos($cache, '/portfolio/'))
{
define('WP_USE_THEMES', true);
require('./wordpress/wp-blog-header.php');
}
else
{
require('./codeigniter/index.php');
}
No Regex needed and it is super fast.