user3237002
user3237002

Reputation: 121

parse url parameters from html to php?

I have a website with a large amount of dynamic content. I have a main template page that then loads the content into it. I have SSI running fine (including to php files) but I cannot get the php files to see the url parameters from the html page. I would rather not convert all my html files to php files if I can help it.

The following code gets to the php file ok (ie displays hello world.) but what do I put for the ???? to get the topic passed from the html url to the included php file?

Ive tried $_GET which works if I call the page directly (ie place getDynamicContent.php?topic=X in the browser) but not when called through #include.

.

for a url: mainPage.shtml?topic=X

.

in mainPage.shtml

//this includes fine - no problems
    <!--#include "commonHeaderStuff.html"-->

//this includes but does not parse variable
    <!--#include "getDynamicContent.php"-->

.

in getDynamicContent.php

//get topic variable
    $topic=????

//return content based on $topic
    echo "hello world";
    if($topic=="X"){echo "You passes an X"}
    else {echo "You passes a Y";}

Upvotes: 0

Views: 380

Answers (1)

Class
Class

Reputation: 3160

You need to use $_GET

if(isset($_GET['topic'])){
    $topic = $_GET['topic'];
    echo "hello world";
    if($topic == "X"){
        echo "You passes an X";
    }else{
        echo "You passes a Y";
    }
}else{
    #no topic
}

Heres an attempt of mine by searching for a way to pass variables to a included from Get url and parameters with SSI

<!-- set default value for SSI variable "topic" -->    
<!--#set var="topic" value="" -->

<!-- get "topic" value from URL --> 
<!--#if expr="$QUERY_STRING = /topic=([a-zA-Z0-9]+)/" -->
<!--#set var="topic" value="?topic=$1" -->
<!--#endif -->

<!--#include virtual="getDynamicContent.php${topic}"-->

I tested this just now and it works also I had to add virtual= to have it work but you might need to change it to file=. you can change <!--#set var="topic" value="" --> to whatever default value you want eg: value="?topic=X"

Upvotes: 1

Related Questions