homepage Welcome to WebmasterWorld Guest from 54.227.40.166
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
passing variables - HTML site - PHP for links
smallcompany




msg:3326492
 5:41 pm on Apr 30, 2007 (gmt 0)

Hi,

We run a site, all pages are in HTML. We have one PHP file called links.php where we store all outgoing links, the code is as follows:

<?

if ($_GET['m'] == "link23") {$link = "http://www.example.com";}
if ($_GET['m'] == "link24") {$link = "http://www.example.com";}

header("Location: $link");
?>

The way how we link to those sites from within our HTML is:

../links.php?m=link23

Now, we have a problem with passing on variables (like keywords). If you visit our site from incoming link that brings …?VARIABLE that VARIABLE needs to be carried through the site and finally passed onto outgoing link if you click onto any.

In the past, carrying VARIABLE through the site was achieved in this way:

Code of .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^index\.htm$ index.php
…and so on for each file…

…until someone told us that code below would save us time on listing each file:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php

We still had to maintain corresponding file in PHP with code like this:

<?

$f=file_get_contents("index.html");
echo str_replace("{kw}",$_SERVER["QUERY_STRING"],$f);

?>

…where {kw} is our VARIABLE

This way, all instances of {kw} within the HTML would get replaced by QUERY_STRING.

As soon as we moved our outgoing links to a single PHP file (great way to manage links, one change affects all site), we lost the way of passing on VARIABLE through the site and out.

Our requirement is to have a solution where we don’t need to maintain one PHP file for each HTML page like in the past, but rather have some PHP code within the page that would pass the VARIABLE throug bthe site and onto our link management PHP file, so our outgoing link at the end looks like this:

http://www.example.com?VARIABLE

[edited by: dreamcatcher at 6:20 pm (utc) on April 30, 2007]
[edit reason] Use example.com, thanks. [/edit]

 

capulet_x




msg:3327134
 7:10 am on May 1, 2007 (gmt 0)

Do you have a database or are you essentially using the PHP file with all of the URLs as your database?
The only thing I can think of right now with your current setup is to build a string for your header location in your outgoing PHP file. You mentioned you were sending variables like this to redirect to a specific URL:

../links.php?m=link23

You could also pass multiple varibles like this:

http://www.example.com/links.php?im=link23&myVariable=example

Then create a string as the header location like this maybe:

<?
$var = $_GET[myVarible];

if ($_GET['m'] == "link23") {$link = "http://www.example.com";}
if ($_GET['m'] == "link24") {$link = "http://www.example.com";}

header("Location: $link/$var");
?>

I hope I understood your question correctly.

smallcompany




msg:3327868
 9:00 pm on May 1, 2007 (gmt 0)

You did understand 100%. We do not use any database but single PHP file to store all outgoing links.

I just played with it a bit and got it to work, thank you so much.

We still have two problems though:

Site is in HTML. In order to get those things passed through we have to parse it as PHP. We do it by using this in .htaccess:

RewriteEngine On
RewriteBase /php/
RewriteRule ^(.*)\.html$ $1.php [QSA,L]

We are not sure if we need [QSA,L], it remained from some previous attempts to make this work.

At the same time, we have corresponding PHP file with this code:

<?

$f=file_get_contents("index.html");
echo str_replace("{kw}",$_SERVER["QUERY_STRING"],$f);

?>

With those two, .htaccess and corresponding PHP file (index.php for index.html), we get our keyword (or any other variable) to replace {kw} string within our HTML.

With your help, we just got {kw} to be passed to our PHP link management file and be forwarded out which is awesome.

..links/links.php?m=link23&myVariable={kw}

Two problems:

First: How do we carry {kw} while people browse through our site? Going from Page1 to Page2 and still have that VARIABLE appended to the end of URL automatically? I say “automatically” as we know how to do it manually, simply by inserting {kw} at the end of each internal URL which may be OK for sites of 5 pages, but not of 150+ or more.

Second: With this setup, we have to maintain a PHP file for each HTML file in order to replace {kw}. How do we do that with one single PHP file that would grab the name of existing HTML file, rather than having us manually fixing that.

Some stuff has been done by this. Thank you so much.

capulet_x




msg:3327952
 10:14 pm on May 1, 2007 (gmt 0)

I'm sure you can write your {kw} varible to a cookie like this:

setCookie("KW_cookie",the_value_of_the_variable,time() + 86400,"/");

The example above will create a cookie named KW_cookie which holds "the_value_of_the_variable" the cookie will be valid for 86400 seconds which, of course, is 24 hours.

When you need your {kw} value in a php page you can set it into a new varible the same way you did in the PHP file. Like this:

$KW = $_COOKIE['KW_cookie'];

But you only have the ONE php page....hmmm...I'msure you could use the cookie in your html.

Also try this:

Change the extension on your html page to php. Don't worry the browser will still recognize the HTML then when ever you call PHP content put the content between PHP tags "<?php.........?>"

This is an example of a PHP include that was put into an HTML page. To get it to work the .html had to be changed to .php which didn't change the html. Only now the browser could read both the html and the php:

Example:

<body>
<div id="wrapper"><?php require_once('gallery.php');?></div>
</body>

There are ways to encrypt the content of the cookie but unless it's sensitive data I wouldn't trouble yourself with it. Also to use cookies obviously your user must have cookies enabled. There are ways to check for this also...but first things first...see if anything I mentioned above will sort out some of your pending issues.


smallcompany




msg:3328014
 11:24 pm on May 1, 2007 (gmt 0)

Now we are getting into hi-tech. :)

Not sure to be honest. I understand the concept, but that's all.

I wish we could get that done with:

.htaccess (already there, may need little tweak)
one PHP file to parse HTML as PHP and for VARIABLE pass between internal pages (missing)
one PHP file for links (already there, no change)

I understand that cookie could take care of VARAIBLE pass. File for PHP parse should have a code that would grab the HTML and replace static VARIABLE string ({kw} in this case) with whatever is coming in from outside.

Thanks.

jatar_k




msg:3328539
 1:07 pm on May 2, 2007 (gmt 0)

well I would adjust your steps

.htaccess - to make html pages parsed as php

a php function included in your pages for VARIABLE pass between internal pages (missing)

one PHP file for links - this is fine

smallcompany




msg:3328876
 5:44 pm on May 2, 2007 (gmt 0)

a php function included in your pages for VARIABLE pass between internal pages (missing)

Yep. That would be it. Easy to maintain as it would be part of a template.

Obviously not easy to make, at least for us at this moment.

jatar_k




msg:3328899
 5:57 pm on May 2, 2007 (gmt 0)

this works, and I guess you could just put this in a file and include that file on any page you wanted this to work on

<?php
echo '<pre>GET vars: ';
print_r($_GET);
echo '</pre>';

if (count($_GET) > 0) { 
echo '<p>set';
} else {
echo '<p>not set';
}
$extravars = '';
if (count($_GET) > 0) {
foreach ($_GET as $key => $value) {
$extravars .= '&' . $key . '=' . $value;
}
}
?>

the link would then be like this

<p><a href='../links.php?m=link23<?php echo $extravars;?>'>../links.php?m=link23<?php echo $extravars;?></a>

see if you can make that bit of code work

the 3 lines at the top is just for testing, it shows you what vars were passed so you can be sure you got them all

smallcompany




msg:3328972
 7:00 pm on May 2, 2007 (gmt 0)

Works!

Variable gets carried through the site.

Now, for each HTML file, we have to maintain a PHP file of the same name.

PHP file “index.php” is like this:

<?

$f=file_get_contents("index.html");
echo str_replace("{kw}",$_SERVER["QUERY_STRING"],$f);

?>

… and so on for each HTML file.

Is it possible to have one PHP file for all so we can replace {kw} with QUERY_STRING within the loaded HTML?

Right now when HTML loads, PHP script for variable carry over loads (the one from above). Would it be possible to have additional PHP code that would replace that {kw} in current HTML?

I am thinking, if PHP script gets loaded at the top of body of HTML, why care about file name to get that replace to work?

How do we say:

$f=file_get_contents("CURRENT HTML FILE");

In addition, if it is possible to do the upper change, how do I configure .htaccess which, without existing PHP file gives “Multiple Choice”?

I have a feeling that even if existing script gets updated so {kw} gets replaced by QUERY_STRING, how to configure server to parse HTML as PHP without corresponding PHP file?

Sorry, I may be repeating same stuff in one post, I am doing my best to describe the current needs and situation.

Thank you very much.

jatar_k




msg:3329369
 1:29 am on May 3, 2007 (gmt 0)

you're missing the point of "parsing html files as php", that's my fault I haven't really explained that

what you do is make a change either using htacces or your host may do it for you. If you control your own server then you make the change to httpd.conf

what this change does is allow you to have php code right in your html files, no need to duplicate the files.

Once this is enabled you can then just put the code in a file, you can then include that file on every page of your site where you want this fucntionality. If you have a template then you can just include it right there.

then in the actual html page you would have something like the last line

<p><a href='../links.php?m=link23<?php echo $extravars;?>'>../links.php?m=link23<?php echo $extravars;?></a>

does that make sense?

this search [google.com] may help

smallcompany




msg:3329425
 3:59 am on May 3, 2007 (gmt 0)

Thanks.

This is what we do right now with .HTACCESS file:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php

This will rewrite current HTML with corresponding PHP which does just this (example is index.html):

<?
$f=file_get_contents("index.html");
echo str_replace("{kw}",$_SERVER["QUERY_STRING"],$f);
?>
Now, instead of RewriteRule in .HTACCESS, if we put this:

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

What should we put right into our HTML instead of having that “file_get_contents…” in separate PHP?

I’ve just tried changing .HTACCESS to that “AddType” and with this in HTML:
<?php echo str_replace("{kw}",$_SERVER["QUERY_STRING"],$f);?>
but it did not work.

{kw} does not get replaced like with Rewrite and separate PHP file for each HTML.

Why?

Thanks.

smallcompany




msg:3329450
 4:46 am on May 3, 2007 (gmt 0)

...update

I got parsing to work with .HTACCESS

The only thing I am missing is how to replace my {kw} within HTML with that $_SERVER["QUERY_STRING"]

I tried this:

<?php echo str_replace("{kw}",$_SERVER["QUERY_STRING"])?>

but {kw} in HTML below this code stays the same.

I would expect {kw} to be replaced by VARIABLE from URL. Example:

www.somesite.com/index.html?VARIABLE

getting close... :)

capulet_x




msg:3329456
 4:52 am on May 3, 2007 (gmt 0)

Consider looking into a javascript string replace if nothing else works.

smallcompany




msg:3329462
 4:55 am on May 3, 2007 (gmt 0)

Thanks.

I am sure PHP works fine. It is just that we are in grade 2 when about PHP. Long way to high-school, no talk about university yet. :)

smallcompany




msg:3329470
 5:17 am on May 3, 2007 (gmt 0)

Dora The Explorer! We DID IT!

All basic requests have been fulfilled.

Things done:

- .HTACCESS configured to parse HTML as PHP
- One line of PHP code in each HTML file
- One PHP file for link management

The code that jatar_k generously written foir us is not being used, primarily because we had no clue what would it do for us. It just happened that we had put back our old configuration with Rewriterule which made us think it started working the new way at that moment.

There is still a lot to be done. We’ll try going hard way so PHP gets better known among us.

jatar_k




msg:3329676
 11:46 am on May 3, 2007 (gmt 0)

glad you got it sorted

>> We’ll try going hard way so PHP gets better known among us.

well you can keep posting here and we'll do what we can to help, you'll be better off in the long run

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved