PHP, includes and paths

Status
Not open for further replies.

wireframed

Ars Legatus Legionis
17,367
Subscriptor
I'm quite the PHP noob, and that's not going to change - I'm unfortunately not a programmer and don't have the time to become one.

However, my site needs to get done, and I'm running into the problem that I'm using a bit of PHP for includes, to make management a little easier, and since PHP sees the root as a totally different directory than standard HTML, I'm having problems using paths relative to root - since the file that the include ends up in might have a different location than another file using the include.

I think I might have gone about includes a little backwards, since I have a file for each page, and that page includes stuff like header and footer from the include dir. (I've seen examples where people do it the other way around and instead include the content from includes).

Still, I figure there ought to be a way to set a path that all PHP files use, sort of like the "base" href tag.
The alternative is to write the full path to the includes every time I'm referencing a php file, but that's a bit cumbersome and doesn't seem like a particularly elegant solution.

A couple complications - I'm on a hosted service, and while I'm probably going to move, they are quite restrictive. I can't get php_value to work in the .htaccess so either they've locked it down, or they're running PHP as CGI (I think?).

Also, I obviously can't get to the php.ini file, because I don't have access to anything outside my user dir.

Are there any elegant solutions, that don't involve changing massive amounts of paths? :)

Thanks,
L.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
More or less everyone has a predictably-accessible configuration file with a base path, and maybe a function there to substitute for include() and require(). It's suboptimal, but it's easy. The alternative is to change your include paths, but most folks don't do that unless they fully control their deployment environments (and even if they do, there's often a fallback somewhere in the code itself). I'm not sure, given what you've described of your setup, whether you can really do that.

However, I would be more worried about why you aren't using a library or a framework that handles this for you. It's probably too late for this project (ouch), but seriously, you really should be using Zend or CodeIgniter or something--especially as someone who recognizes that they're a newbie. Rule #1 of programming: it's been done better by someone more experienced. Might want to keep that in mind for the future.

Or, if it's just a website and you're literally only using it for the equivalent of SSI, your site should probably be in a CMS like Drupal or ExpressionEngine. (N.B.: Wordpress isn't a CMS, but might be appropriate too.)
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
It's my personal company website. I don't use a lot of fancy PHP, and I didn't want to take the time to get all the stuff coded up in a CMS so I've basically built a HTML/JS site, and then used some PHP for SSI, dynamic menu and a few other things I've cobbled together.

The site is never going to be huge, since it's a small design company. Most of the pages will consist of cases and references.

I figured a full-blown CMS was probably not going to be worth it. Perhaps I should reconsider, but that would probably mean spending a lot of time setting up templates, implementing the JS stuff (Cufon, Galleria, Sphider search, AJAX category sorting etc.), and I really just want this done and running. :)

At some point I might implement it in a CMS, but given I have no problems doing the editing in Dreamweaver (which is mostly adding cases), I didn't want to spend time doing it now.

L.

Edit:
I've looked at few CMS's, also because I have clients who may want a full website solution. I usually hire freelancers for that, or in the cases of the really large clients I collaborate with a bureau and only do the design, but if the site is small enough, it might be worth it to do the job myself. I've looked at something like Pulse CMS because it looks VERY simple both to use and implement in. For a few customers that have contacted me for design for small, 10-20 page sites, it might be useful.

Suggestions for other CMS's I should look at?
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
"Didn't want to take the time" - bzzt. Once you learn how to wield a CMS it's considerably quicker than doing anything by hand. Seriously. If it fits within the CMS's sphere of usefulness, it's a snap. The key is to find a CMS that has a sufficiently broad sphere of usefulness, and a good community for learning how to get there.

Templates? Dunno about shit like Joomla (and I'm not using "shit" to mean "stuff," Joomla is shit in the excremental sense), but in Drupal you more or less just seed your HTML with a few PHP snippets and break it up into page.tpl.php, node.tpl.php (for content nodes), and block.tpl.php (for embeddable blocks). Takes me no time, once my HTML/CSS are right. Implementing JavaScript? for table-sorting? Why do that by hand when the CMS comes with table-sorting already available (hi, Drupal), and if you don't like the default behavior there's almost certainly already a module for it?

This is the future. We don't need to reinvent the wheel every ten minutes.

Dreamweaver...christ. I'm sorry, and I'm trying not to be a dick (note this down, it's a first), but you're really heading toward the Doing It Wrong Cliff at a very rapid pace. Were I you, I would learn Drupal (as it's incredibly flexible, although users may need some training - I've never had to spend more than five minutes doing that sort of thing) and probably pick up Using Drupal or whatever to get started. I've never even heard of PulseCMS, but they immediately fail to impress when I look at their (lack of) community and (lack of) extensions.


Going the CMS route is a good idea; in the long run, it's almost always cheaper to just Do It Right.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
I'll take a look at Drupal. I looked at Joomla since a client has a site in Joomla that they asked if I'd be able to do some updates on, mostly polishing design and stuff like that, and it looked incredibly confusing, with terms like "Articles" for what is basically just pages. I get it's geared for fairly large sites, but it just didn't seem very intuitive at ALL.

As for Dreamwaver, I use it as an HTML WYSIWYG editor, since I'm a designer - I don't code sites in notepad. :p It works nicely for that IMO.

"Didn't want to take the time" - bzzt. Once you learn how to wield a CMS it's considerably quicker than doing anything by hand. Seriously.
Sure, but realize I don't generally sell finished websites. I sell design and/or project management, and then talk to coders how to get it implemented.
So GENERALLY speaking, every hour I spend learning how to set something up in a CMS is an hour I might not get any return on, unless it converts DIRECTLY to savings on a single site. I.e. (again generally speaking, since there could be exceptions down the road) learning how to work in Drupal needs to pay off on ONE site, for it to make economical sense. It's not like spending 50 hours learning the ins and outs of Drupal can be amortized over the next 10 projects where I save an average of 8 hours each. It has to convert to 50 hours saved on this single site.

As such, I initially decided against CMS's. I'll give Drupal a look, since it sounds like it's a lot easier to work with than the other CMS's I have experience with. (Joomla and a proprietary CMS developed where I used to work before starting my company).

L.
 

beeblebrox

Ars Legatus Legionis
11,208
Subscriptor
Blacken++

You might check out http://www.websitebaker.org -- it's a smaller CMS that still handles a lot of what you can throw at it, is easy to get into, and still works like a champ when the user(s) want to edit stuff themselves. I use it a lot, but then I tend to do sites like http://www.nettlen.com and http://www.deusdesigns.com with that backend.

I'm getting into Drupal myself, though, just because 1) it's a good buzzword to have when talking to clients, and 2) websitebaker is nice, but it's not Drupal.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Lestat: no offense, but a lack of forward-thinking and amortizing over multiple projects is absolutely bizarre. For 3/4 of projects, you can cut out "coders" (coders. ugh. but hiring real developers is expensive, I guess) entirely with a halfway decent CMS. I'm a developer by trade and I would go entire projects without touching code of any kind, because the necessary tools already exist in my CMS or in quickly installed modules.

I'm so glad I'm out of dealing with that horseshit altogether.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
Blacken00100":3nvo9ufu said:
Lestat: no offense, but a lack of forward-thinking and amortizing over multiple projects is absolutely bizarre. For 3/4 of projects, you can cut out "coders" (coders. ugh. but hiring real developers is expensive, I guess) entirely with a halfway decent CMS. I'm a developer by trade and I would go entire projects without touching code of any kind, because the necessary tools already exist in my CMS or in quickly installed modules.

I'm so glad I'm out of dealing with that horseshit altogether.
Not necessarily. Besides, 70% of the projects I design are custom coded/developed because they are more like web-apps than websites. :) I don't generally do anything code related, and when it's not just my own site, I hire freelancers.
I totally support forward-thinking and amortizing cost over multiple projects, but that's only possible when the experience relates to other projects, and right now it relates to one and MAYBE a couple others over the next couple of months.
You have to understand, I generally just don't work with the code at all. I design, project manage and let developers or coders do the coding.

Also, I've now spent about 2 hours just trying to install Drupal and still haven't got past the WSOD, so I'm not exactly thrilled with the investment so far. :p

That's for another thread though. ;)

However, it still underlines the point, that given I'm not a programmer and don't have plans to be one - I have my plate full with design and 3D - I'm going to spend more time doing trivial stuff like trying to figure out why Drupal doesn't install right, and why it won't throw up error codes, than a programmer would, and that's time I can't bill anyone.

Were I a programmer, it would be valuable lessons and experience. For me, it's just time I wasted trying to get something to work instead of DOING work. Which is why, while I appreciate the many ways a good CMS makes things simpler, it also means I'm a bit ambivalent about it because I know from experience that getting something like that set up and running can end up taking 20 hours, which is thousands of dollars unbilled. :)

L.
 

beeblebrox

Ars Legatus Legionis
11,208
Subscriptor
Lestat":19rq5izk said:
Were I a programmer, it would be valuable lessons and experience. For me, it's just time I wasted trying to get something to work instead of DOING work. Which is why, while I appreciate the many ways a good CMS makes things simpler, it also means I'm a bit ambivalent about it because I know from experience that getting something like that set up and running can end up taking 20 hours, which is thousands of dollars unbilled. :)

Need help setting up your CMS?
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
beeblebrox":2nixeqxa said:
Need help setting up your CMS?
Through trial/error I found that some of the settings in the .htaccess file screw with the server - probably because the host I'm using is very restrictive. Which is also the reason I'll probably move hosts soon. (Which sucks because I just paid for another 12 months and they don't issue refunds).
It's cheap hosting, but it has sufficed for what I needed up till now. I don't get significant amounts of newbiz through the site anyway, so it's more a reference for potential clients I've already talked to.
Which is also why it's hard to defend poring lots of hours into it - I've already spent enough time that I'm far from sure I'll recoup the costs, but being a designer, I feel I should have a nice-looking portfolio. :p

Now to figure out how to implement designs. It doesn't look too hard, but I'll probably finish my current semi-dynamic frankenstein site, and then look at porting it into Drupal if it looks like it's fairly easy to do.
I desperately need to get something online as I'm having a relatively large influx of new clients, and have to constantly appologize for my 6 year old, completely out-dated website. :)

L.
 

KarlShea

Ars Tribunus Militum
2,277
Subscriptor++
Lestat":33ddsv2q said:
beeblebrox":33ddsv2q said:
Need help setting up your CMS?
Through trial/error I found that some of the settings in the .htaccess file screw with the server - probably because the host I'm using is very restrictive. Which is also the reason I'll probably move hosts soon. (Which sucks because I just paid for another 12 months and they don't issue refunds).
It's cheap hosting, but it has sufficed for what I needed up till now. I don't get significant amounts of newbiz through the site anyway, so it's more a reference for potential clients I've already talked to.
Which is also why it's hard to defend poring lots of hours into it - I've already spent enough time that I'm far from sure I'll recoup the costs, but being a designer, I feel I should have a nice-looking portfolio. :p

Now to figure out how to implement designs. It doesn't look too hard, but I'll probably finish my current semi-dynamic frankenstein site, and then look at porting it into Drupal if it looks like it's fairly easy to do.
I desperately need to get something online as I'm having a relatively large influx of new clients, and have to constantly appologize for my 6 year old, completely out-dated website. :)

L.

Once you start getting into theming in Drupal, you should check out some of the starter "reset" themes like Zen... you'll spend less time overriding things and more time implementing your actual design.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
KarlShea":zzitrzrr said:
Once you start getting into theming in Drupal, you should check out some of the starter "reset" themes like Zen... you'll spend less time overriding things and more time implementing your actual design.
Thanks, I'll check that out. I really just need as basic a theme as possible since I probably won't use it for anything other than figuring the structure out, so I can see which tags I need to implement my designs.

L.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Still, I figure there ought to be a way to set a path that all PHP files use, sort of like the "base" href tag.
The alternative is to write the full path to the includes every time I'm referencing a php file, but that's a bit cumbersome and doesn't seem like a particularly elegant solution.
To answer the first question...

Why not define a variable "rootdir = blah/blah" at the top of each page and use that in your include calls? (not a PHP developer at all, no idea what the syntax actually looks like) You still have the problem of editing every file if that directory changes, but you only have to change one line in each file, rather than umpteen lines per file and inevitably miss one of them.
 

beeblebrox

Ars Legatus Legionis
11,208
Subscriptor
ronelson":29k6gcwa said:
Still, I figure there ought to be a way to set a path that all PHP files use, sort of like the "base" href tag.
The alternative is to write the full path to the includes every time I'm referencing a php file, but that's a bit cumbersome and doesn't seem like a particularly elegant solution.
To answer the first question...

Why not define a variable "rootdir = blah/blah" at the top of each page and use that in your include calls? (not a PHP developer at all, no idea what the syntax actually looks like) You still have the problem of editing every file if that directory changes, but you only have to change one line in each file, rather than umpteen lines per file and inevitably miss one of them.

All he needs to do there is set up a "master" include file, where he lays out some system-wide information:

$rootdir = 'http://www.yourproject.com/whatever';

It's also not a bad place to store things like database info, format your date/time, etc. Then, he just needs to call that file at the first line on every page with an include statement:

include('master.inc');

That way, he only needs to change "master.inc" in one place to have the changes affect all his pages.

Or, use a CMS. Preferably, use a CMS. But the include file method works as well.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
I settled for using:
<?php include ($_SERVER["DOCUMENT_ROOT"] . "/path/to/file.php"); ?>
for the includes. It's not very elegant unfortunately, but it'll suffice for now.

And yes I realize a CMS would make some of these things easier. I'm not adverse to using CMS's, and ironically *I* was the one trying to get my old employer to realize that they needed to do cost/benefit analysis, and stop trying to develop things like a project management app, which they spent countless hours doing instead of just installing Redmine or something like that.
(Also tried to get them to do more data analysis and projection, so they didn't spend 2-300+ man-hours adding features that no one knew whether would result in more sales... :rolleyes:)

For the current project, the rationale is that I've already spent time implementing these things on flat HTML files to get the dynamic functions I need, so instead of starting over and going the CMS route (which is Duke Nuke'em Forever all over again), I'll finish up the site, and then when it's online and running, look at porting it to something like Drupal.
I can't really estimate the timeframe needed to learn Drupal well enough to implement my design, so I'd rather get the project done and online instead of postponing it indefinitely until I have the perfect version ready.

Avoiding feature-creep is at least as important as doing it right. :)

Just wanted to make clear that I'm not ignoring Blacken and Beeblebrox' advice, and I'm also not oblivious to the advantages of going the CMS route (heck my old employer WAS a CMS developer, among other things).

L.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Lestat":1v5b4400 said:
I settled for using:
<?php include ($_SERVER["DOCUMENT_ROOT"] . "/path/to/file.php"); ?>
for the includes. It's not very elegant unfortunately, but it'll suffice for now.
Not a the best idea. Whenever possible, your includes should be below the document root for the purposes of security. (If someone types in http://domain.com/path/to/file.php, what happens? Are you sure that happens? Could they do anything nasty if it doesn't do what you expect? The answer is usually at least "probably," so tucking it below the docroot is usually the best method.)

As far as Drupal goes--honestly, given your designer-centric outlook, I might look at ExpressionEngine instead of Drupal. I use Drupal heavily, but it is tailored more toward a programmer. There are approximately a metric fuckton of modules for both that make it a lot easier to do most common (and lots of uncommon) tasks.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
If you're creating custom php, and not integrating it into an existing project, it's pretty easy to structure it so the main parts of it are outside the document root. As Blacken00100 said, this is the preferred way to deal with it. (If nothing else; getting any files containing database passwords, or other sensitive information, out of the document tree is strongly suggested).

On one project I was working on the only thing in DOCUMENT_ROOT is obj.php

Code:
<?php
  require('../bin/obj.php');
 ?>

In this case the applications obj.php acts as a broker and handles all requests.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
Hm, yeah, I see the reasoning, and have run into it in other places, but I don't think my host lets me get into anything below document root...
Of course I could just create the structure myself and edit the folder permissions... Hmm...

As for ExpressionEngine, it's not expensive, but the fact that it's ForPay sets the bar a little higher, because I can't just install it, mess around with it and then decide if it's worth paying for. It would be nice if they had a demo - it would suck to find out that I couldn't run it on my current host anyway, and so wouldn't be able to use it before I move host etc.

Perhaps I'll throw a few hours after Drupal seeing if I can figure out the system, and if it proves cumbersome, I'll look at EE.

I'm assuming Drupal is the best for-free choice - I've looked at Joomla, and I was far from impressed with the usability of their interface. To me, it abstracts too far away from the structure of the website.

L.
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
Blacken00100":3tg5qblu said:
If you email EllisLab's support, you can get a free trial.

And it's PHP. If Drupal works on it, EE will work on it. Probably better; EE has a tendency to handle older versions of PHP better.
I was thinking more of the restrictive nature of my host. (Which, as mentioned before, I'm going to move from).

I was trying to get a PHP/CGI script installed so I could let my clients upload files - I found a great upload script for relatively cheap which was very customizable and did what I needed and lots more - unfortunately my host won't even allow CGI scripts to run.

Similarly, the .htaccess file of Drupal breaks the CMS because my host blocks some of the functions and settings they try to set, so I had to completely delete it to get the CMS to install.

So while EE *might* work, I'm not sure I can count on it.

I'll try contacting them about a trial, though. It's not like $99 is a lot if it works well, it's more than $99 is a lot if I waste time AND money and it doesn't work in the end. :)

L.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Well, keep in mind that CGI =/= PHP. PHP can be run as CGI (typically via FastCGI, which is...generally a good idea), but standard CGI scripts are often blocked for security purposes. A PHP script that doesn't require CGI (I've never seen one, but that doesn't mean much) should run essentially everywhere.

The lack of .htaccess is fucking weird, though. Dreamhost (who I've used forever) is just "hey, yeah, don't break it for other people, otherwise have fun."
 

wireframed

Ars Legatus Legionis
17,367
Subscriptor
Blacken00100":1sqksi4z said:
Well, keep in mind that CGI =/= PHP. PHP can be run as CGI (typically via FastCGI, which is...generally a good idea), but standard CGI scripts are often blocked for security purposes. A PHP script that doesn't require CGI (I've never seen one, but that doesn't mean much) should run essentially everywhere.

The lack of .htaccess is fucking weird, though. Dreamhost (who I've used forever) is just "hey, yeah, don't break it for other people, otherwise have fun."
This is the script I was considering purchasing:Filechucker.
The appeal was that it seemed easily customizable, was very user-friendly (given I also do a lot of interface design, it's not completely irrelevant... :p) and could be configured so clients just type their company name and hit upload, and it'll automatically create a folder for their files and throw me a link notifying me they uploaded something. I think it might also be able to clean files out regularly via a CRON job, but I'm not positive.

However, it uses a CGI script, that I was told in no uncertain terms by my host that they didn't allow. It's ONE reason I'm strongly considering moving hosts. :)

I looked into EmotionEngine, and it seems they don't offer trials at all anymore, but instead money-back guarantee. Not quite as good, but certainly much better than a "Too bad". :)

Given that the site is already done, the idea that I could convert the page layout to a template simply by inserting tags where needed in a flat HTML file is immensely appealing - even using the Zen skin for Drupal it seems it's going to take a few hours of research just to get a handle on the basics, and that's not even considering the time to actually start implementing the design, porting over stuff like the Galleria slideshow which I use extensively, getting search configured (I've already set Sphider search up and configured the resultlisting etc.), getting category functions sorted etc.

On the positive side, a CMS would allow me true tagging/taxonomy, and probably custom search-views. (I.e. tag cases with a category, and make them clickable, so clicking the "Interface Design" tag would produce a search listing with all the cases with that tag and a thumb etc.).

I guess I'll have to move to some CMS solution in the near future to get the things I'd really want to play with.

L.
 
Status
Not open for further replies.