PHP Website Basics Directory Structure - php

I have a newsletter 3rd party system. It has been working really well however I have come across an issue which I'm hoping is a really easy fix.
So this is the root structure of my website
FTP SERVER
/index.php
/news.php
/shane/shane_about.php
/mailist/globals.php
/mailist/mailbar.php
/mailist/mailbar8.php
So I have an input box which allows me to register for a newsletter. It works a treat on index.php and news.php however screws up the whole page on shane_about.php because its in a child directory shane.
Now the problem is in the globals.php
<?php
$main_dir = "maillist/";
$website = "http://www.mysite.com/";
$relative_string="index.php?page=mail&";
$absolute_path="/hostingcompanyserver/something/something/something.com/maillist/";
$lang="lang_english.php";
So I changed the variable $main_dir to "../mailist/"
This then worked on the shane/shane_about.php, then index.php and news.php had the same issue (it screwed up).
I'm sure the solution is very basic, any thoughts would be appreciated.
Thanks Chris

Change $main_dir to an absolute path.
If it's the same as $absolute_path you won't have this problem.
$main_dir = "/hostingcompanyserver/something/something/something.com/maillist/";
The problem at the moment is index.php and news.php look inside maillist as defined by $main_dir. shane_about.php looks inside /shane/maillist/.
You then told it to go up a directory to maillist with ..maillist/. This meant shane_about.php was looking in the right place but index.php and news.php weren't.
By using an absolute path all files look in the same place.

Related

Integrating Codeigniter and Wordpress

I designed a site using Wordpress, and have been trying to get a Codeigniter form app integrated with it, with all the corresponding URLs.
I put the CI app in the root folder of the Wordpress site. The folder is named "Chile" (it's a series of forms separated by LATAM countries). I changed the .htaccess file to include /chile where necessary. I also added:
require '../wp-load.php';
to the CI index.php file.
I can now load some of the essential urls in the CI app, but they don't seem to be routed properly.
the base url is set to:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])).'/';
the site is www.dermusikplatz.com. the form is located under www.dermusikplatz.com/chile/applications which i can reach by manually entering that. but when i try to submit the form, the url appears strangely as www.dermusikplatz.comapplications/save. In general, the urls constructed by the controllers and views just don't seem to be set up properly.
I've looked up the documentation regarding routing and have tried creating a $route rule. but i'm not sure what to route from and to what exactly. I've also seen various threads that suggest using MY_URL_helper to change the value of 'site_url' to 'ci_site_url', to avoid any redundancies between the value that Wordpress and CI have for that, but i'm not sure where to even start with that, even after checking out the helper documentation.
I've also tried going in and changing each path value in statements like these from:
$this->load->view('applications/form');
to:
$this->load->view('/chile/applications/form');
but I understand that makes little sense as that directory doesn't exist. I assumed that changing the .htaccess file and the base_url would cover it, but there seems to be much more to it than that.
I'm a little lost here, and not super familiar with Codeigniter.
If anyone can help set me in the right direction at the very least, I would really appreciate it. Thanks.
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
Change that to:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'] . '/Chile/';
It is worth noting that if you are hosting on Linux the 'Chile' must be the exact same case as your folder is called. So, if it is 'Chile', do not call it 'chile'

Codeigniter with single controller error 404

I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!
As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.

codeigniter only pages to an existing website

My website structure somewhat looks like below
css/
lib/
js/
index.php
profile.php
products.php
checkout.php
orders.php
invoice.php
I have added a codeigniter folder in there ...
codeigniter/application/
codeigniter/application/controllers/
codeigniter/application/controllers/mycontroller.php
and other files
I can access CodeIgniter stuff by going to mywebsite.com/codeigniter/mycontroller etc fine.
However, I want to get rid of /codeigniter/ part from the URL. So I was wondering if it is possible to create a whitelist of the files which are CodeIgniter specific? For example, if the URL is mywebsite.com/mycontroller then it does CI stuff otherwise it looks for the plain PHP code file. I have only a couple of CI controllers and loads other non-CI files.
Any ideas?
I think you could use .htaccess to rewrite URL's that don't contain .php, css, lib and js. Something like:
RewriteCond %{REQUEST_URI} !^(\.php|css|js|lib)$
RewriteRule (.*) codeigniter/index.php/$1
So:
http://example.com/css/test.css
stays
http://example.com/css/test.css
(as will all requests to css|lib|js. You can append more things here for the rewrite to ignore)
http://example.com/controller/method
becomes
http://example.com/codeigniter/index.php/controller/method
You can test it out here: http://htaccess.madewithlove.be/
More on rewriting: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Short-term Solution
You can start by simply converting the index.php file into a controller and name it whatever you wish:
<?php
class New_default_controller extends CI_Controller {
public function index()
{
// home page stuff here
}
}
Alter the route.php file and set your default controller so that simply visiting your site will trigger the proper controller:
$route['default_controller'] = 'new_default_controller';
Apply the instructions for Removing the index.php file
Now calls to www.mysite.com/profile.php will access the profile.php at your root and calls to www.mysite.com/new_future_page will call your new_future_page controller.
Please let me know if any of this is confusing or you get stuck.
Optimal Solution
I wanted to leave a comment above but this would have been impossible to show as a comment.
You will have to take your PHP files and put them in the controllers folder like this:
codeigniter/application/controllers/profile.php
codeigniter/application/controllers/products.php
codeigniter/application/controllers/checkout.php
codeigniter/application/controllers/orders.php
codeigniter/application/controllers/invoice.php
Please go through and do the Tutorial before continuing any further. Specifically the Static Pages section will help you in achieving your goal.
You will have to convert your current PHP files to follow the flow of CodeIgniter

redirect to mobile website using PHP

I have use this to generate this code:
<?php
require_once('mobile_device_detect.php');
mobile_device_detect(true,false,true,true, true,true,true,'http://m.mydomain.com',false);
?>
But the only directions are to "copy and paste this code". Um.. copy and paste where? Do I need to create a new php file? Is this index.php? What if I already have an index.html file?
EDIT: I understand that I put mobile_device_detect.php in the root of mydomain.com. My question is where to put the above php code.
Copy and paste this at the beginning of your PHP based pages that you want to detect the visitors for their device. If your server parses HTML files as PHP which I doubt then add this in your HTML files as well. If you're just building the website then yes you need this in files which are parsed by the PHP engine for example: ".php".
If you paste this in page that is HTML and not parsed by the server you'll see this same code as output which will do nothing. In order to have it working you need it in PHP files.
If your script is well written and well structured you may need to include it in only one place. It all depends how your website is structured.
------ UPDATE ------
Why you shouldn't be using this class? It have a special license which is not absolutely free.
Instead you can use this simple class: https://github.com/serbanghita/Mobile-Detect
Download Mobile_Detect.php
Include the file at the top in your PHP page where you want the device to be checked:
// Include the mobile device detect class
include 'Mobile_Detect.php';
// Init the class
$detect = new Mobile_Detect();
// And here is the magic - checking if the user comes with a mobile device
if ($detect->isMobile()) {
// Detects any mobile device.
// Redirecting
header("Location: http://your_redirected_url.com"); exit;
}
Creating rewrite rules for using html extension.
If you still want to use '.html' as extension just create rewrite rule that will rewrite your .php as .html. Or otherwise said create your_page_name.php and add the PHP code there. Create .htaccess file in the same DIR and add the following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^your_page_name.html/?$ your_page_name.php [L]
Save, close! Now you should be able to use your php page with .html extension. To access your page now just type: http://yourdomain.com/your_page_name.html
Simple as that!
Suggestion: If I was you I'd add the rewrite rules in the web server's config file. It will be faster and more secure. But that's another lesson. If you decide to use this method just search the Stack.
Copy and paste the code anywhere you want. Just make sure the function is defined on any page that needs it.
You should either buy the script mobile_device_detect.php from the site or use a free method called pay with a tweet option.. Go to the download page and you will see them there..
ok, in case this helps someone, here are the details of what's working for me:
Create an index.php file with just this:
<?php
require_once('mobile_device_detect.php');
$mobile = mobile_device_detect();
// redirect all mobiles to mobile site and all other browsers to desktop site
if($mobile==true){
header('Location:http://m.yourdomain.com/');
}else{
header('Location:http://yourdomain.com/index.html');
}
exit;
?>
Drop the mobile_device_detect.php file in the root of your site.
Then, add this line to your .htaccess file:
DirectoryIndex index.php index.html

Codeigniter : what if I want both URL with index.php and without index.php work

I want both index.php included, and index.php excluded from my URIs to work smoothly on my site. I need it because of uploadify not working, and it is giving me a HTTP error.
It works if I have changed this
$config['uri_protocol'] = 'AUTO';
to this
$config['uri_protocol'] = 'QUERY_STRING';
, but then whole system is not working properly.
For your assets, first, on config.php set the $config['base_url'] to your website URL.
Then, when linking to assets on which you do NOT want index.php, just use the function base_url
<?php echo base_url("assets/uploadify/uploadify.swf"); ?>
// Generates http://URL/assets/uploadify/uploadify.swf
Follow the link in the comment from Indranil above to remove index.php from your URIs by modifying your .htaccess file.
Here is the address again for reference:
http://codeigniter.com/user_guide/general/urls.html
Keep in mind too that this can vary from host to host in the way that it needs to be formatted, so follow up on that with your hosting service. Most usually have guides that show you how to configure this properly.
I'd also like to know how you're using your links, but I'll go ahead and explain what I think may be happening to you. If you're doing any kind of link/anchor or redirect() using the URL Helper in CodeIgniter, then make sure you're using the right function to execute those actions. Here's what I mean:
The site_url() function/method will always include index.php in your URIs. Here's an example using the site name http://example.com as your website's domain.
<?php echo site_url('home') ?> will turn into http://example.com/index.php/home
While base_url() function/method will always exclude index.php unless you include it. Here's an example using the site name http://example.com as your website's domain.
<?php echo base_url('home') ?> will turn into http://example.com/home
and
<?php echo base_url('index.php/home') ?> will turn into http://example.com/index.php/home
Keep in mind that using these functions/methods are only effective AFTER you have properly modified your .htaccess file.
Here is the link that shows you a more in-depth difference between the site_url() and base_url() functions.
http://codeigniter.com/user_guide/helpers/url_helper.html
I hope this helps!

Categories