Forum Moderators: phranque
an index.php file with "if" statements:
if($v == "1")
{
$w->someFunction();
include("sometemplate.php"):
}
elseif($v == "2")
{
...
etc..
I need to set up a global variable in the index.php file, which will be passed to all of my methods..
(basically that global variable is supposed to be a prefix for my sql tables)
What I can't get done is that the global variable is not being passed to particular statements, with an except of the last "else" statement..
(I tried to set it up as a global var, tried to use define function.. I have register_globals set to ON..)
maybe it's worth to mention, that the global var is a variable taken out of subdomain using an .htaccess file:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?variable=%2 [L]
so, I'd like to pass that variable to all of if/elseif statements:
if($v==1){
$variable = "somevalue";
$w->someFunction();
}
Does anyone know how to define a global variable for all if/elseif statements?
Thanks.
However I really recommend using reference:
function someFunction($variable) {
$variable = 2;
return "Whatever";}
...
$w->someFunction(&$variable);
...
Moreover you can discard the if/elseif/else structure and change it to switch:
switch ($v) {
case 1: $w->someFunction();
include("sometemplate.php");
break;
case 2: ... break;
}
Best regards
Michal CIbor
ok, here's the deal:
If you could take a look at the piece of the code of an .htaccess file you will see that a variable called "variable" is being passed to index.php file - and this part works fine.(cause when I type a URL "subdomain.mydomain.com" - the subdomain value is assigned to the "variable"..) - and this, as I said earlier, is a result of calling the "else" statement..
so, the problem is, that when I type, let's say:
subdomain.mydomain.com/index.php?v=2
( which calls elseif ($v=="2"){ some action..} )
then the variable is not passed.. ($variable = "";)
so, looks like the $variable is not being passed along with $v..
So, I assume I'd have to tell php to pass along that variable to all my statements (not to functions, because that's already one step ahead..)
hope that makes sense.
/*this gives nothing*/
if ($v == "1") {
$variable = $_GET[variable];
echo $variable;
}
/*this gives nothing as well*/
elseif ($v == "2") {
$variable = $_GET[variable];
echo $variable;
}
/*this gives a value of the variable*/
else
{
$variable = $_GET[variable];
echo $variable;
}
so rewrite is fine, besides that's jatar_k's code..
as far as using switch I don't see anything against using it, but that's not the point here - first I wanna get it working..
I want to set up subdomains so that name.domain.com calls a php script at [domain.com...]
in order to do so, I have an .htaccess file which handles that:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?subdomain=%2 [L]
and..the index.php file also includes whole bunch of if if/elseif statements (which I'm going to change to switch).. depending on what condition you choose you're calling different functions..
I want the variable $subdomain to be some kind of config variable - it's supposed to be a prefix for my sql tables..
so if you choose
[myname.domain.com...]
it selects all data from a table called myname_sqltable or something like that..
if you choose [myname.domain.com...]
id deletes all data from a table called myname_sqltable..
etc..etc..
so, as I said there is plenty of if statements in the index.php file - and I can't pass the $subdomain variable to them..
How to define that subdomain variable so that it will be dynamically changed, depending at what URL you are (subdomain)..?
Has anyone ever had a similar issue?
Look at your rewrite rule again. I am no regex whiz, but I don't see where you are allowing for $v in the rewrite.
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /index.php?subdomain=%2 [L]
I read that as
rewrite "www.subdomain.domain.com
to
"index.php?subdomain=subdomain"
If I am correct, then Birdman's advice might well fix it.
WBF
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^([^.]+)\.html$ /index.php?subdomain=%2&v=$1 [L]
and it worked out.
however, that solution does not satisfy me, cause I have couple more parameters being passed along with the $v variable, like id's, etc..
I was not familiar with the QSA flag before, but that solution seems to be an ideal one in this situation, but unfortunately it didn't really cause anything - $v is still not being passed..
BTW, looks like now the post became more appropriate for the apache section.. Can you guys move it over?
Thanks.
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^([^.]+)\.html$ /index.php?subdomain=%2&v=$1 [QSA,L]
Jim
I did it what you suggested - and it worked out.
if ($v == "test") {
if($id == "1" && $catid == "2")
{
$sub = $_GET[subdomain];
echo $sub;
}
I type [somedomain.domain.com...]
At first I was a little bit concerned cause it's not as much SE friendly as I wanted it to be, but basically I added some more rewrite rules:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^url1_(.*).html /index.php?subdomain=%2&t=test1&id=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^url2_(.*)_(.*).html /cfadmin.php?subdomain=%2&v=test2&cat_id=$1&id=$2 [QSA,L]
etc..
And it seems to work fine.
The only concern I have is the repeating condition statement.. Is there any way to handle it in a more elegant way?
Once again, many thanks Jim.
If you only have a few rules, then repeating the RewriteCond is actually faster. Once you get beyond, say, three rules, then you can use a different construct that is less obvious and more complex.
Instead of
RewriteCond <condition1>
RewriteRule <rule1>
RewriteCond <condition1>
RewriteRule <rule2>
RewriteCond <condition1>
RewriteRule <rule3>
RewriteCond [b]!<c[/b]ondition1>
RewriteRule .* - [S=3]
RewriteRule <rule1>
RewriteRule <rule2>
RewriteRule <rule3>
This technique is more useful as the number of 'shared' conditions increases, and as the number of rules increases. For fewer than three or four rules, it's just as well to use the longer, more straightforward approach.
Be aware that using negative logic like this introduces complications to combinatorial conditions; If you used to test for Cond1 AND Cond2, you will need to convert that to test for NOT(Cond1) OR NOT(Cond2). If this subject is new to you, a search on "DeMorgan's theorem" may be useful.
Jim
I've followed your instructions quite thoroughly, however looks like I'm loosing the variable ($subdomain) passed through backreference(?)
here's my code:
RewriteCond %{HTTP_HOST}!^(www\.)?([^.]+)\.domain\.com
RewriteRule .* - [S=3]
RewriteRule ^list_(.*)_(.*).html /index.php?subdomain=%2&t=list&catid=$1&id=$2 [QSA]
RewriteRule ^edit_(.*).html /index.php?subdomain=%2&t=edit&id=$1 [QSA]
RewriteRule ^show_(.*).html /index.php?subdomain=%2&t=show&id=$1 [QSA,L]
I'm assuming this has got something to do with DeMorgan's theorem..otherwise you wouldn't mention that, right? However, honestly I don't see where could I apply these rules, cause I can't see any AND's and/or OR's..
Can anybody tell me what I'm missing in that code?
Thanks.
# Extract subdomain if present
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# If html filetype and subdomain is present, store it into user-defined variable and skip next rule
RewriteRule \.html$ - [E=subd:%2,S=1]
# No subdomain or non-html filetype, so exit
RewriteRule .* - [L]
# Rewrite html files using stored subdomain
RewriteRule ^list_(.*)_(.*).html /index.php?subdomain=%{ENV:subd}&t=list&catid=$1&id=$2 [QSA,L]
RewriteRule ^(edit¦show)_(.*).html /index.php?subdomain=%{ENV:subd}&t=$1&id=$2 [QSA,L]
Have fun! :)
Jim
Ok Jim, once again thanks for the great lesson, and hopefully I won't have to be a pain on your butt anymore:)
Have a good one,
Thanks.