3

I didn't notice this question on the site already and I am having a hard time with it.

I simply want to run a php script at the top of a html page that will check for the existence of a cookie, making in essence a password protected website. If the person browsing has the cookie, they are allowed to continue, otherwise they are redirected to a login page. I am pretty new to web development, but I was led to understand that....

<html>
I am in html!
<?php
//Code goes here
echo "I am in php!";
?>
I am back in html!
</html>

Would work...But I only get the output of the html sections.

7
  • Made a mistake with posting code, bear with me... Commented May 24, 2012 at 20:52
  • 1
    Ha. We made edits at the same. I've rolled back to mine, since it keeps it all in a single code block. :) Commented May 24, 2012 at 20:55
  • Stephen, does your server even have PHP running? Where are you testing this code? PHP is server-side. A browser cannot execute PHP by itself. Commented May 24, 2012 at 20:58
  • You need PHP installed on the machine you are testing in order for your code to be interpreted... Commented May 24, 2012 at 20:59
  • I have php installed on the server side and it is working fine. I am able to go to php pages that I have generated and do whatever - but for some reason I can't seem to get it to run in the html pages. Thank you to whoever fixed my original post by the way. Commented May 24, 2012 at 21:02

2 Answers 2

4

PHP is processed server side, not client side... HTML on the other hand is processed client side.

What you should be doing is rather than running php in an html page is to run html in a php page.

The reason to this is that if you send both the content and the security mechanism to the client, it would be easy for someone just to bypass the security - look here for example and simply look at the source code.

If you wanted proper PHP, You would have something along the lines of:

if(isset($_COOKIE['MyCookie'])){; 
// Escape PHP and put html here...


//go back to php.
}

You will need a server that can process PHP, but, I should say that there are much better way of protecting pages, such as .htaccess and similar which I urge you to go down.

Sign up to request clarification or add additional context in comments.

2 Comments

I have seen .htaccess mentioned once in a while, but I never found very good documentation on what it was or how to use it effectively. I have been going down the path of using setcookie(...) and then checking from there if they client (at least, I understand the client is the one that has the cookie) has the cookie to continue to the page or not. I didn't think of just running the html in the php instead of the other way around...That I know I can do.
Your first use of the .htaccess file: AddHandler application/x-httpd-php5 .html, this allows a PHP script to be executed from *.html files
1

Normally a .html page will not run php codes. But we can run PHP code by creating a .htaccess file in the same folder where the .html file is held. in .htaccess file add this code

AddHandler application/x-httpd-php5 .html .htm

this code can vary depending on the php version and server you are using

1 Comment

Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.