I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.
Related
I have 2 web pages. Both have PHP scripts. Playing.php has a table showing the last 20 songs played on my shoutcast server. I'm trying to get that displayed on my index page.
Index.html: http://www.deamon.org
The page I'm trying to add is
http://www.deamon.org/scxml/playing.php
Can I do this with include function and echo or is there a better way.
The quickest and easiest way is to use an iframe:
<iframe src="http://www.deamon.org/scxml/playing.php"></iframe>
My best suggestion would simply be to include it with a simple include statement or using an iframe
<?php include("link to file"); ?>
<iframe src="link to file"></iframe>
You could use a PHP include you can do this by adding this line of code into the index file.
First you would have rename index.html to index.php and then place the following code in-between the tags where you would like the page displayed.
<?php include '/scxml/playing.php'; ?>
My suggestion would be to include the .php file into your index page. However including PHP code into an HTML type page requires reconfiguring your server. Instead, do the following:
Change your index.html file to an index.php file. It will be read and rendered the same way without requiring any changes. (make sure that the index.html file doesn't exist on the server so that the index.php file automatically gets picked up
add the following line of code into your index.php file:
<?php include('path/to/playing.php'); ?>
Hi I am a newbie to web development. I am trying to display echo message from a php file in html page.
PHP File:
<?php
echo "Hello";
?>
Other file:
<form method="get" action="latest.php">
</form>
Note: These both are two different files
I am completely new to this. I don't know where I am going wrong.
Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.
Step 1:
Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website
AddType application/x-httpd-php .html
Step 2:
Go to Your HTML file and add an include statement where you want your PHP file to be included
<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?>
</form>
Step 3:
All done, now go check output in browser.
Just put the code together:
<form method="get" action="latest.php">
<?php echo "Hello"; ?>
</form>
Everything inside the Tags will be interpreted as PHP, no matter where it is. But make sure the ending of the file is .php!
May be try this..
<form method="get" action="latest.php">
<?php include('php_file.php'); ?>
</form>
Please note that other file should also be a .php file. if it is an html file you either need to change it to .php or you need to make ajax request.
You can't execute php codes in a file ending other than .php.
But you can have HTML code displaying in a .php file.
So I would recommend you change the extension to .php.
You can simply have
<?php
$name = "Harry Potter";
?>
<h1>Hello, <?php echo $name;?>!</h1>
Also you need a web server software and php processor. WAMP or XAMP may help you on that.
I am editing my answer since some are really picky.
In general you can't execute php in files which dont have the .php extension. But with some tweaks and line changes you can. I am just stating that you can't since, you mentioned that you are new to web development and I would like to make things simpler rather than confusing
If you want to execute php code in .html file extension than u need to do some htaccess changes.since file extension plays a major role ,it only tells the browser how to handle or parse a particular page.
You can create a .htaccess file at the root folder of your website and in the htaccess file add this line:
AddType application/x-httpd-php .html .htm
If you only plan on including the PHP on one page, it is better to setup this way:
<Files yourwebpage.html>
AddType application/x-httpd-php .html
</Files>
This code will only make the PHP executable on the yourpage.html file, and not on all of your html pages.
If you are ok with changing your file extension to .php than your work will be quite easy.you can simply include a php code between html code.like this
<form method="get" action="latest.php">
<labe><?php echo "hello" ?></label>
</form>
Its so simple you can just put php tag and write php code even inside the html
<form method="get" action="latest.php">
<div><?php echo "Hello";?></div>
</form>
Whenever you echo something, it is printed on html page only.
If you want to echo inside a particular html element, just put the code there. For example,
<html>
<body>
<div class="myDiv1"><?php echo "This is myDiv1"; ?><br></div>
<div class="myDiv2"><?php echo "This is myDiv2"; ?></div>
</body>
</html>
Hope this helps...
I have some PHP code in my .php file which is showing as it is in the browser. As in, I want the code to execute on form submit, but its showing as plain text in the browser.
<?php
if(isset($_POST['ss-submit']))//submit button name
{
//Describing the Leads object and printing the array
$describe = $connection->describeSObjects(array('Lead'));
print_r($describe);
?>
Below it is an HTML form which displays correctly.
now this .php file is included in a .tpl file.
You can keep your both files different.
In case of smarty the php file is executed first process data and then with the help of assign function you can assign your output from php to the tpl file.
Try as much as you can process data at the php page only
<?php
if(isset($_POST['ss-submit']))//submit button name
{
//Describing the Leads object and printing the array
$describe = $connection->describeSObjects(array('Lead'));
$smarty->assign('lead_array',$describe);
?>
Replace $smarty with your smarty object
Replace lead_array with a name with which you want to access array in .tpl file
now on the .tpl page you can simply write
{$lead_array|print_r}
make sure your form on the .tpl page post on the same php file
it will print your array
This is a sure solution try it once and you will get desired output.
in a .htaccess file
AddType application/x-httpd-php .php .html .tpl
beware this parses all tpl files as if they were php. A templating engine may be better
I'm creating a LAMP stack and just started learning PHP. I'm having trouble with my Hello World program. On my server I have a file called index.html. This is the contents of that file (which is basically copy pasted from an online guide):
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
echo '<p>Hello World</p>';
?>
<b>Here is some more HTML</b>
</body>
</html>
When opened on a web browser, I expect my output to look something like this:
My Example
Hello World
Here is some more HTML
Instead, it looks like this:
My Example
Hello World
'; ?> Here is some more HTML
Why is that extra "'; ?>" there? I'm probably making a simple mistake. I've tried accessing index.html on chrome, firefox, and safari, same result each time.
PHP does not work in HTML files, try renaming it to index.php
Apart from that the code is fine (maybe some best practices like separating HTML and PHP, but you'll get to that later)
The reason you don't see the whole PHP code on your screen is because the browser is trying to parse the element <?php *** ?> as if it where valid HTML (like <span>). If you check the html-source, you will see all of your code.
As someone in the comments below mentioned, there are ways to make PHP work in HTML. You should not do this, unless you are very aware of what you are doing. It is not a default setting and it should stay that way. If you should be able to use PHP in HTML files, .phpfiles would not have to exist.
When you expand your knowledge, you will start separating HTML files from PHP files and make PHP include templates. Two separate files for two separate goals
Rename the file to index.php.
In addition to this, the PHP code must be interpreted by a WEB SERVER(Apache in your case).
For example(if you are using XAMPP on Windows), and your php file is present in the webcontent directory of your server's document root(htdocs), then type the following in your browser
http://localhost/htdocs/webcontent/index.php.
Like others said, you must rename index.html to index.php, but if you must use index.html, put the line below into your .htaccess file, so you can process .html files as .php.
AddType application/x-httpd-php .html
I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...