PDA

View Full Version : Call .php file within .html


bigbikkuri
05-10-2006, 04:26 PM
I'm working on an education site for kids and I'm trying to workout how to have a user login and propagate a session and variables throughout the site.

Looking at sites like digg.com and amazon.com, everyone has a " <username>'s page " button or display with personalized info, but the page is either a .com or does not have the file extension displayed in the url.

I'm thinking in order to do this I want to call a seperate php script that has access to my SESSION array so I can make personalized links and content, and that would then echo the html code to display on the mainpage. But so far it seems to do that I need to embed php code into my page, and that means I'll need an index.php which is pretty unsightly (or at least, nobody else seems to be doing this and or hiding the fact - and I want to too).

My question is, is it possible to execute a php script from within straight xhtml? I know in the form tag there is the action attribute that can be php, but is there a way to just say "in this div, but the results of myfile.php" and have it be pure xhtml?

GJay
05-10-2006, 04:43 PM
You're asking more questions there than you think...
To do things with sessions, you have pages with session_start() at the top, and then put things in $_SESSION.

Apache can be set up to treat a url ending with a / as meaning /index.php, rather than the normal /index.html

An alternative is to use url-rewriting (mod_rewrite with apache) that tells the server to treat, for example www.example.com/gjay/ as www.example.com/index.php?username=gjay which puts 'gjay' in $_GET['username'] for the script to use.

bigbikkuri
05-10-2006, 05:04 PM
oh-hoh, url rewriting? Wow, I didn't see that listed anywhere on php.net - but you said it was an apache thing so I guess they wouldn't talk about that would they?

I've discovered that I can change my httpd.conf to treat html files as php files - so that it basically parses every page for php code, which is fine by me.

Now if I can just understand session propagation and variable propagation. It is very hard for me to wrap around my head that I can still have variables available to me even though I've gone to a completely different page. And I thought making a 'simple login' would be 'simple.'

GJay
05-10-2006, 07:58 PM
page1.php

<?php
session_start();
$_SESSION['test']='hello';
echo 'var='.$_SESSION['test'].'<br /><a href="page2.php">Go to page 2</a>';
?>

and page2.php

<?php
session_start();
echo 'var (still)='.$_SESSION['test'];
?>


is about all you need to know :)