# Include PHP File in HTML Code



## r3drock3t88 (Jan 13, 2007)

Hey all,

What I am trying to do here is I would like to have an external PHP file named "column_left.php" which will store the contents in the left column so I don't have to update every single page individually with the same information. I am using dreamweaver by the way.

I have my entire website laid out using html and CSS and all that good stuff, then I created a page and saved it as "column_left.php" and then I used the

<?php include("column_left.php"); ?> approach to enter the column in a table in my html code where I need it to be. After doing this, I saw a little "php" box in the table that I placed the code in the design view of dreamweaver, but when I load my webpage nothing shows up in that column?

I then created an "index.php" page and used all the same html code, then placed the include file in there and when I loaded the webpage the left column showed up like I wanted it to. But in the design view of dreamweaver my website looks completely different and everything is out of place.

What I want here is to be able to use my already made HTML Pages and include a simple left column for some "update" news and what not using PHP. I don't know why this isn't working though! Any help is much appreciated! THank you all!


----------



## cpscdave (Feb 25, 2004)

as a rule of thumb you cannot trust dreamweaver with spacing whne you have included php files. 

Especially if you're php file ouptputs a bunch of html that'll affect the spacing. Dreamweaver is just smart enough to know that there is a php file there not that it'll affect the spacing in any way shape or form.


Best is to install apache and php locally and then look at the changes off of the served page


----------



## r3drock3t88 (Jan 13, 2007)

Well, I bought a webhost so I have the server and all that good stuff setup already. After I add the PHP to the html in dreamweaver I save it and it autoloads the file to the server, then I test it on the web. 

My Problem is that when I load the PHP in my HTML file and load it in the web it does not show up at all. I did the proper include and all for the php file, but not too sure why nothing shows. The thing is though that if I create a "index.php" file and load all of my HTML into that, then load the php include code into that as well and upload it to the server it works fine, but then the design of it in dreamweaver looks like complete crap, so I can't work on it like I use to.

Anymore help is much appreciated


----------



## cpscdave (Feb 25, 2004)

Ahhh I think I know what's going on.... know it all too well. (I've done this many MANY times myself)

Just during the development phase (as soon as you go live take this out) add to the top of your php code:

```
ini_set('display_errors','on');
```
What I think is happening is you have an error in your php code thats causing it to die  This shoudl tell ya if you are and give you clues on what you need to do to fix it.


----------



## r3drock3t88 (Jan 13, 2007)

Hmm, I believe my PHP Code is coded correctly. There isn't much to it because I am just testing it, but I think I found my problem. Apparently I need to add these lines to my .htaccess file ... 

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

This will parse my PHP code in the HTML Documents. But now I have a new problem! After I add these lines to my .htaccess file and upload it to my server, my entire website is completely down and it says I have a "500 internal server error". This only occurs when I add those lines. Any ideas?? I am pretty sure that is what I need to do in order to embedd php in my html.

thanks again!


----------



## Eriksrocks (Aug 7, 2005)

Just make sure that you save the main page with an extension of .php so that it will parse the PHP and you are good.


----------



## r3drock3t88 (Jan 13, 2007)

Yes, but wont that just simply parse the php in the main page and not the other pages that I need it to be parsed in?


----------



## MMJ (Oct 15, 2006)

Each page that has php code in it (<?php ?>) needs to have a .php extension.


----------



## cpscdave (Feb 25, 2004)

naming a file .php tells the server that it needs to parse the php.

otherwise it'd look at

```
<?php
require_once "somefile.php";
?>
```
and not know what to do.

The require_once/require/include functions act just as if you yourself had taken all the contents of the file and put them inline.


----------



## r3drock3t88 (Jan 13, 2007)

MMJ said:


> Each page that has php code in it (<?php ?>) needs to have a .php extension.


I was told that you could parse the php into an HTML document using this code.

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

You have to put the code in the .htaccess file. I've read this on several websites, not so sure how credible they are? But that is what I hear you can do. However, when I do this and save the .htaccess file to my server I get an internal server error 500. :-/ not real sure why.

Also, when I change my page to .php my entire template gets screwed up in the design view of dreamweaver... I can't design when it looks completely different then what I think im designing lol. Any ideas?


----------



## MMJ (Oct 15, 2006)

r3drock3t88 said:


> I was told that you could parse the php into an HTML document using this code.
> 
> RemoveHandler .html .htm
> AddType application/x-httpd-php .php .htm .html
> ...


You can do that but you shouldn't because than each file will be parsed for php which would reduce performance.

If you want to zip whatever you have and post it here.


----------



## r3drock3t88 (Jan 13, 2007)

Hey MMJ, Thanks for the suggestions. I guess I'll go with your approach since performance is a major factor. I'm in no rush to have the website finished, So I don't mind starting over. It's all a learning experience  . Thanks for the offer too! I appreciate it. I will just restart everything and redesign my website since my template keeps getting all funky. And I will save all my pages as .php ... Now I just have to learn how to have a members login section! If you'd like to help me out with that, it would be much appreciated  If you get the time that is, thanks in advance!


----------



## MMJ (Oct 15, 2006)

Okay, no prob.

Did the includes work out?


----------



## r3drock3t88 (Jan 13, 2007)

MMJ said:


> Okay, no prob.
> 
> Did the includes work out?


Yes sir they did, I realized why my dreamweavers design view was getting all funky. In my php included file i was adding html and css which was conflicting with the original index.php's html and css. Once I removed the html and css from my php include file, everything worked fine . Thanks a lot for helping me out, I now realize how that all works and its a good feeling fixing things! haha.

If you get time / the patience to help out a newcomber with how the login and members section workout on a website...I would be very thankful! But only if you're up for it.

Thanks in advance!


----------



## MMJ (Oct 15, 2006)

I'm glad it worked out for you. 

As for the login script, will you have a register page? If so you are going to need a db.

Look at this Google search:
http://www.google.com/search?q=php+login

As you can see there are lots of ready scripts out there. If you need help setting them up just post here.

Here are some good ones:
http://php.about.com/od/finishedphp1/ss/php_login_code.htm
http://www.mtdev.com/2002/07/creating-a-secure-php-login-script/


----------



## r3drock3t88 (Jan 13, 2007)

Hello MMJ,

Thanks again for all your help. I took a look at the search results and came upon this page.
http://evolt.org/article/comment/17/60265/index.html

The script looks pretty good to me, but of course I'm not that advanced so anything looks good to me! haha.

I think i might need to take you up on the offer of helping me set this login script up. I don't understand how the whole process works. When the user isn't logged into the website, I would like a link somewhere to say "Login or Register Here". After the user logs into the website, I don't understand how once they login the website changes from a guests website to a members website. It's hard for me to explain but I hope you have somewhat of an idea as to what I mean. Like if I want for only members to be able to comment something, or for members to be able to add an article. I don't understand how the website knows to check for that, then if the user IS logged in it allows them to do said things.


----------



## MMJ (Oct 15, 2006)

You would like a registration page, right?

If you think you will need it you can use a CMS which already has all that.

If not, go to the link you posted and put each code in its respective filename on your site.

If you have phpmyadmin than craete a db, user, and pass. Then go to the db and run this query on it.

CREATE TABLE users (
username varchar(30),
password varchar(32));


----------



## r3drock3t88 (Jan 13, 2007)

I can always add more required information into that query, correct? And once I create it, what will I need to do after that? I need to connect to my database, then use the "register" page that is already created to also connect to my database and register my user. After that I will want the user to be able to login using the login form. I have the ideas, just not the know hows! hahaha. I really appreciate you helping a newcomber out, I realize it can get frustrating and you've helped me quite the amount of times in the past.


----------

