Missing PHP Session in .html file - php

I'm trying to migrate a Webshop to a new Webserver. It is working perfectly fine on the old Webserver, however, when I try to login at the Webshop (index.html) it returns to the homepage, not logged in as the Session Variable is empty.
Then I noticed, when I opened another Site called request.php) the Session Variable was set and I was logged in.
So I tried several things, I renamed the index.html to index.php and the session was there.
My question is now: Can I get the Session in .html files, too (as on the previous Webserver) or do I have to rename all my .html files?
Note: The index.html file contains php code as well and is parsed as php, just the session variable is empty.
Thanks in advance for every answer!

if you are using a Apache Server, try add this to the config file.
AddType application/x-httpd-php .html

You need to set a handler in your web server to treat html files as php files.

The files need to be renamed to have a .php extension on them, even if they include html.
Simply include your php code before the html starts, like so:
<?php
//code goes here
?>
<html>
<body>
//etc...
This way, you can load up your session variable and any other PHP variables you need, then they'll be accessible in the html as well.
If you have several pages that will need the same thing (session variable), you could put the session code into a php file, something like 'session.php'.
Then, at the top of every page you're converting from html to php, put this code at the top:
<?php
require_once("session.php");
?>
Now all of your pages will have access to the same information, and it helps to cut down on the code too.

AddType application/x-httpd-php .html
In your .htaccess file will do the job, still I believe it's rather unsafe to do it :)

Related

Require php line of code not working

Very new to all of this and having some issues using a line of code.
All i am trying to do is have my html file have a require statement that pulls a php file however it is not working. Please help.
<?php
require 'localhost/php/loginscreen.php';
?>
"All i am trying to do is have my html file have a require statement that pulls a php file however it is not working".
That to me suggests that you're using an .html file as a base file to pull in an .php file.
Unless you've instructed your server to treat .html files as PHP, you would need to rename that file to a .php extension.
Edit. Consult the following Q&A on Stack on how to do this:
Using .htaccess to make all .html pages to run as .php files?
Your syntax for require 'localhost/php/loginscreen.php'; is incorrect.
You would need to either use a full system path: (as an example, replace that with your system path).
require '/var/usr/public/php/loginscreen.php';
or a relative path:
require 'php/loginscreen.php';
and using http://localhost rather than a possible file:/// url in the browser.
"Strange as there is no error. as i refresh my html file nothing happens."
Had you viewed your HTML source, you would have most likely have seen code, rather than parsed (PHP) directives. That would have been the reason why you did not see any errors, least not php-wise.

.php with HTML versus .html with PHP

When I am starting to build a site that is going to require both HTML and PHP, should I be making a .html file with PHP in it (as in the file would be, say, index.html but within it there would be various tags)? Or, should I be making the files .php files and simply include HTML within it (as in the file would be, again say, index.php and it would start as PHP and I would simply intertwine HTML)?
TL;DR: Should I be weaving HTML into .php files or weaving PHP into .html files?
It should be a PHP file with HTML "weaved" into it. By default if your server sees an HTML file it does not think it needs to process scripts on the page and will render it. If it sees a PHP extension, it knows it needs to run through the PHP Processor.
You can modify your htaccess to allow HTML to be rendered through the processor, but there really is no need for you to be modding that, especially if you are a beginner.
You use PHP files with HTML in it
You should "weave" html into php files That way you know for sure your code will work on any server, and not just on servers that renders html files as php.
You need to specify in your .htaccess file to be able to parse PHP inside of a .html file. The easier way to go is just to make everything .php.
Inevitably, when you get more comfortable with PHP, you'll learn that you'll always have a little PHP in the file (like a require or something), so best to plan for that.
If you are new to PHP, I would recommend creating files with the .php extension, as the .php file can be executed by default. Depending on your server configuration, you may have to add some .htaccess directives to allow php code to run in an .html file.
If you like .html extensions, you can use .phtml files for templating your system, but only for the files that containing html code. And I prefer to use .php files that containing only php code like classes etc (this is what Zend or similar libs do).

Making theme files .html

They have some php code within (if, endif, variables), but essentially they are html files.
Do you think is a good idea to use the .html extension and prevent direct access to them trough .htaccess, so the php code is not visible to anyone ?
Is it safe?
can you test the script?
As far as I know the PHP is just server-side so after the server "do its thing" it doesn't show on the users computer (when he tries to see the source code).
Javascript and HTML are displayed but not the php.
I have used the .htaccess file to block some other files in the folder such as the DB credentials and such that I had named .inc (for include). If you block the html with the htaccess noone is going to be able to see the webpage.
I hope I understood it right! (And clarified it as well)
Just have an .htaccess with deny from all in the views folder... ;p
Tho if you have files like images or css that you want loaded from that theme folder then by all means rename the view to .php and put as this on the first line:
if (!defined("RUN")){die('No direct access');}
Obviously define('RUN',true); in your config.

php echo fails to print

I am new to PHP. I am developing php under Redhat.
The index.html is like this:
<?php
$myvar="AAAA";
echo $myvar;
?>
But nothing is output in the browser.
Any help?
Rename the file to index.php and it should work.
If that fixes it the problem was that the webserver (presumably Apache) knows that .php files should be rendered with PHP but it correctly ignores .html files.
From what i see, the file is interpreted as HTML and not PHP. If you see the source of the page, you will see your PHP code.
Try renaming the file to index.php and if your server is set up properly, it should interpret the PHP code.
Change the file extension from .html to .php and your code will work fine.

Can you add a .php file to your .html file?

I have a website made and i have saved the file as .html, I now want to add a .php file. Is this possible as i have tried so hard to do this. Id appreciate any help given :)
You can do several things.
Rename your .html files to .php.
Tell your web server to pass .html files to the PHP interpreter. In Apache you'd do this by putting AddType application/x-httpd-php .html .htm in your .htaccess file or your Apache configuration.
If you have installed PHP, you can rename your file to *.php and insert PHP codes!
You want to put PHP code inside a .html file? You'd have to configure the webserver to treat the .html file as a PHP script. Technically, there's no such thing as a PHP script. There's only files which have at least one <?php ?> code block within them. As such, ANY file can contain PHP code. The problem is getting that file into the PHP environment.
Unless your webserver is told to hand the .html file over to the PHP interpreter, you cannot execute the PHP code contained within it - the raw code will get sent out as part of the html page.
Just rename your .html file to .php, if your webhost supports it.
If you run your own server you can even configure PHP to also run .html files, but it's not recommended because of the performance penalty on all html files.
Yes, you can include php file into html file. i m sure that this link will help you.
http://php.about.com/od/advancedphp/p/html_php.htm

Categories