Page 1 of 2

newb question: why php?

Posted: Wed Dec 07, 2016 8:12 pm
by Sophia
I promise I have done some googling, but still feel a bit lost. (Largely it took me to this robotic anime explanation.) I do notice that most folks around here have their sites coded in PhP. My understanding is that PhP is more about user customization, which doesn't seem obviously in play in shrine making to me, though obviously I am new to this and may be wrong.

Is it because many of you are designers? What is the stronger features that php has over html that you find useful for shrining / personal collectives?

... to be totally honest a smidgen of this is intimidation in learning it. I've wanted to code forever and am looking forward to the challenge, and will probably plod along anyway. Just wondering if anyone had thoughts as to the possible rewards.

Re: newb question: why php?

Posted: Wed Dec 07, 2016 11:11 pm
by Masao
I think most of us use it for its "dynamic" features, specifically the include/require functions. That way, we can include our "header" and "footer" files and not have to individually change each page whenever there's an edit of the layout.

On our domain collectives, it's not a huge deal, but this can come in really handy if we wanted to change the layout of a 10+ page shrine. We can just edit the header and footer files, and the change will reflect on all the pages that include those files.

Code: Select all

<?php include 'header.txt'; ?>

(html/content of your specific page)

<?php include 'footer.txt'; ?>
and your header and footer files can be:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="header></div>
<div id="content">

Code: Select all

</div><!-- end #content -->
</body>
</html>
Other benefits include adding conditions so that if something is true, we can specifically code our layout display something. Like how a lot of wordpress blog themes have their current page highlighted through a class="current" in the navigation html, which is dynamically inserted with PHP.

I hope that makes sense :ack:

Re: newb question: why php?

Posted: Thu Dec 08, 2016 1:09 am
by Stefi
I use PHP for the exact reason Masao gave. I basically just have to go in and edit the header and footer one time if I need to switch the layout or make minor changes.

This is a really good question though! I had been coding for a good couple of years before I was taught the benefits of using PHP. I also didn't understand why at first since it wasn't something I can right click, 'view source' on. If you follow Masao's template though, making the transition is actually really, really easy.

If you do decide to use PHP, just be aware that there are a few vulnerabilities, but they are pretty easy to avoid. For example, using a file.php extension is considered a generally safe practice.

Re: newb question: why php?

Posted: Thu Dec 08, 2016 2:15 am
by Masao
Stefi wrote:If you do decide to use PHP, just be aware that there are a few vulnerabilities, but they are pretty easy to avoid. For example, using a file.php extension is considered a generally safe practice.
Yeah, the one drawback with PHP, since it is a scripting language, is you could go wrong if you follow random really out-of-date tutorials.

Generally though, for the file extensions of your header and footer files, it can be any text file (from .php to .inc or .txt) as long as you don't have any sensitive information, for example, if you were to include a file that had login and password for your database. If you want to keep it private, name the file with a .php, so that your server will interpret the file not as plain text (and display everything), but process it as PHP, so that only want you want to print out, will in fact show.

The other easy vulnerability is if you dynamically include files (like a page.php?name or a page.php?x=name), if you don't check that it's only including files on your server, someone could try to load an external file by typing it in the browser page.php?[insert full url of a malicious file]

Re: newb question: why php?

Posted: Thu Dec 08, 2016 4:11 am
by Lethe
(wtf @ robotic anime explanation OMG I DON'T EVEN UNDERSTAND IT)

Yeah, I'm pretty sure the majority of us "only" use PHP as Masao and Stefi described, as it makes our lives easier. 8D /doesn't actually know much about PHP aside from a few snippets...

I hope I'm not derailing the topic, but this might be handy to know for shrines: The dynamic inclusion that Masao mentioned in the last paragraph is something I (and others, I assume) often use so that I don't have to make a new file for each page. I find it easier to keep an overview and edit pages when I only have to load a few files, rather than open a bunch of them, and then I can just search them with ctrl+f.

Say you have a file called section1.php:

Code: Select all

<?php include("header.php"); if (!$_SERVER["QUERY_STRING"]) { ?>

(HTML content of section1.php)

<?php } elseif ($_SERVER["QUERY_STRING"] == "page1") { ?>

(HTML content of section1.php?page1)

<?php } elseif ($_SERVER["QUERY_STRING"] == "page2") { ?>

(HTML content of section1.php?page2)

<?php } include("footer.php"); ?>
That makes three pages in one file. I use it here, for example (note the URLs).

I also include this snippet Masao wrote for me in the footer so that I don't have to manually update the year in copyright notes:

Code: Select all

<?php $y="2016";echo (date('Y')==$y)?($y):($y.'-'.date('Y')); ?>
(2016 here is the start date of a site. Once it's 2017, the snippet on the sites will automatically change to display "2016-2017".)

... Just a few neat things to share. <_< >_>

Re: newb question: why php?

Posted: Thu Dec 08, 2016 1:37 pm
by Robin
Joining with the "PHP Includes Layout Brigade," LOL! It helps SO MUCH not to have to include all the layout info on every page, and sometimes I've even used includes to keep files from getting too long as well (such as this type of character page to keep the sidebar content separate from the main content).

Also @Lethe omg that is so cool!

Re: newb question: why php?

Posted: Thu Dec 08, 2016 3:32 pm
by dubiousdisc
Hee hee, I saw this being posted and was like "I'll wait until Masao writes it better than I ever could" and then there it is! Thank you Masao :D

Re: newb question: why php?

Posted: Thu Dec 08, 2016 4:53 pm
by Sophia
Per usual this has been immensely helpful - thank you so much everyone! I am much more clear on the utility now

Re: newb question: why php?

Posted: Mon Dec 12, 2016 6:21 am
by Eunice
Hi Sophia! I still use html. I tend to use php if I'm making a fanlisting as I use Enthusiast, and you have to use php for that. But for a static shrine, I just use html and fiddle about with css.

Re: newb question: why php?

Posted: Mon Dec 12, 2016 9:49 pm
by Sophia
That's fair Eunice! And probably wise for me, given I've got two HELP ME threads in the coding section. >>