PDA

View Full Version : Apache gurus...


sarettah
05-06-2005, 10:39 AM
This might be totally stupid question, but then again, it may not.

Is there a way to create a server side variable ?

What I am trying to do (and I am just starting to look at this, haven't even searched on it yet..lol) is create a counter that can be referenced by a script. The objective is to avoid having to open and close a file. Instead I want a variable that will reside in memory and will be available everytime the script is called for.

I need to be able to increment and decrement the counter via script.

I can do it easily enough by reading the variable in, inrementing it, writing it back. But it seems like it would be better if I could just reference a memvar if possible.

Thanx in advance

Geekily yours

Sarettah :yowsa:

Mr Benn
05-06-2005, 11:03 AM
Speaking for Apache 1.3.x, the only way you can share variables accross child processes is through shared memory (shmget() etc). To accomplish this you're going to have to write an Apache module in C. I wrote one a few years ago that used shared memory for tracking some things, if you're having trouble i can dig it out next week.

I don't see much wrong with just reading/incrementing/writing the result to a database though..

If you use an Apache module, you could have more trouble in the future if you want to quickly put your script on other servers etc.

I've got a feeling Apache 2.x can better support "global variables" accross child processes, as i think it uses pthreads instead of fork()ing. I'm not sure though, you'll have to look into it :)

sarettah
05-06-2005, 11:18 AM
Thanx :)

I think though, that it is probably not apache that I need to address, but rather php. :)

Been thinking about it a bit, in ASP this would be accomplished with a global defined in the global .asa file. My experience says that anything I can do in ASP I can do beeter in PHP :) So, I am figuring that there is something I can define through the php configuration that might work :)

I didn't want to go to a db or a file just for speeds sake. This is for the contest landing page script. I already have a test version worked out using a mysql table with just the counter in it.

I think, even though it is not really noticeable, that this is a lot more overhead (all things relative) than is necessary to complete the task.

The entire thing is basically

start
Get hitcount - if in file, open file, read var. If in mysql, open db, select from tbl
hitcount ++ - increment, if file write back, close file. if db, update, close db
if hitcount>5
hitcount=1
endif
case
hitcount=1
redirect to team1 url
hitcount=2
redirect to team2 url
hitcount=3
redirect to team3 url
hitcount=4
redirect to team4 url
hitcount=5
redirect to nicks shave script ( :yowsa: )
endcase
end

DrGuile
05-06-2005, 11:27 AM
Originally posted by sarettah@May 6 2005, 10:19 AM
Thanx :)

I think though, that it is probably not apache that I need to address, but rather php. :)

Been thinking about it a bit, in ASP this would be accomplished with a global defined in the global .asa file. My experience says that anything I can do in ASP I can do beeter in PHP :) So, I am figuring that there is something I can define through the php configuration that might work :)

I didn't want to go to a db or a file just for speeds sake. This is for the contest landing page script. I already have a test version worked out using a mysql table with just the counter in it.

I think, even though it is not really noticeable, that this is a lot more overhead (all things relative) than is necessary to complete the task.

The entire thing is basically

start
Get hitcount - if in file, open file, read var. If in mysql, open db, select from tbl
hitcount ++ - increment, if file write back, close file. if db, update, close db
if hitcount>5
hitcount=1
endif
case
hitcount=1
redirect to team1 url
hitcount=2
redirect to team2 url
hitcount=3
redirect to team3 url
hitcount=4
redirect to team4 url
hitcount=5
redirect to nicks shave script ( :yowsa: )
endcase
end
I would do it with a db. It would be lighter with a apache module, but it seems like alot of trouble for no reason.

50k hits, even all on the same day, isnt alot to handle for apache/php/mysql

Nickatilynx
05-06-2005, 11:27 AM
Originally posted by sarettah@May 6 2005, 07:19 AM
Thanx :)

I think though, that it is probably not apache that I need to address, but rather php. :)

Been thinking about it a bit, in ASP this would be accomplished with a global defined in the global .asa file. My experience says that anything I can do in ASP I can do beeter in PHP :) So, I am figuring that there is something I can define through the php configuration that might work :)

I didn't want to go to a db or a file just for speeds sake. This is for the contest landing page script. I already have a test version worked out using a mysql table with just the counter in it.

I think, even though it is not really noticeable, that this is a lot more overhead (all things relative) than is necessary to complete the task.

The entire thing is basically

start
Get hitcount - if in file, open file, read var. If in mysql, open db, select from tbl
hitcount ++ - increment, if file write back, close file. if db, update, close db
if hitcount>5
hitcount=1
endif
case
hitcount=1
redirect to team1 url
hitcount=2
redirect to team2 url
hitcount=3
redirect to team3 url
hitcount=4
redirect to team4 url
hitcount=5
redirect to nicks shave script ( :yowsa: )
endcase
end
my work here is done....

:)

sarettah
05-06-2005, 11:36 AM
lolol...thought you would appreciate that Nick :)

Well, I think I found what I need. Gonna go play with it a bit. Using Zend (right under "creating variables" in the php manual...what a weird place to find it, ya know ? :huh: )

Anyway, yeah, I know I can do it with a db, thats what my test is using. But, I like eloquence :yowsa: And when I know I should be able to do something I have this overriding need to figure it out. :blink:

Thanx Guys :) I'll let you know how it goes :)

Mr Benn
05-06-2005, 11:40 AM
Ah, you're right about PHP.. it has functions for working with shared memory. Didn't think of that to start with as the thing i'd previously done had to be a 100% self contained module :)

If you want to do it without Zend, then the PHP SHM functions are at http://www.php.net/shmop

Edit: Remember that if your server dies, you're going to loose all of your data if it's only in memory. Might be worth at least writing it to a DB every so often as a backup.

sarettah
05-06-2005, 12:28 PM
Well... Not having much look with the zend version. Getting a syntax error off of the ZEND_SET_SYMBOL() function and I have tried about every different variation I can think of. And of course, the only example I find when I search is the PHP manual (which is where I grabbed the model I am using came from).

Thanx MR Benn for the shmod ref. I have it cookmarked for later investigation.

But, I have wasted enough time on this. I have a working model using mysql, guess I will work that to the live version :)

Thanx again :)


Now, got to go work out the trade script..... Nick, was that every 3rd that you get, or every 4th ? :yowsa:

sarettah
05-06-2005, 01:24 PM
Landing page script.

http://www.bntnews.com/contest/rotateit.php


(edited version of it)

//run the make table script at the beginning of contest to create count table and initialize it

$hitcount=0;
$db = mysql_pconnect(hostname, username, password);
mysql_select_db(dbasename,$db);
$sql_str="select * from contest_hit_counter";
$result=mysql_query($sql_str, $db);
if (mysql_num_rows($result)===0){
echo "Didn't get the record back
";
}
else
{
$hitcount=mysql_result($result,0,"hitcount");
if ($hitcount>3){
$hitcount=1;
}
else
{
$hitcount=$hitcount+1;
}
$sql_str="update contest_hit_counter set hitcount=" . $hitcount ;
$result=mysql_query($sql_str, $db);
If ($hitcount===1){
echo "redirecting to team 1
";
echo 'header("Location: http://www.team1url.com/")';
}
If ($hitcount===2){
echo "redirecting to team 2
";
echo 'header("Location: http://www.team2url.com/")';
}
If ($hitcount===3){
echo "redirecting to team 3
";
echo 'header("Location: http://www.team3url.com/")';
}
If ($hitcount===4){
echo "redirecting to team 4
";
echo 'header("Location: http://www.team4url.com/")';
}
}

Trev
05-06-2005, 01:27 PM
I'm getting the feeling that someone else was in there refreshing at the same time as me because I went weird, then back to normal :)


Nice work Sarettah :okthumb:




















So are you adding the skim feature after everyone has seen it :yowsa:

Mr Benn
05-06-2005, 01:37 PM
Originally posted by sarettah@May 6 2005, 09:25 AM
Landing page script.

http://www.bntnews.com/contest/rotateit.php

Heh. Good job i'm not in the contest with that as the landing page script ;)

With it set up how it is now, if you wanted to get nearly 100% of the traffic from it all you have to do is have a script on your server look for when a hit comes in, and as soon as it does, automatically send 3 hits of your own to cycle it round to your site again. Would just need some extra checks in there aswell to keep it in sync for when a real hit arrives in the middle of your generated hits and messes the cycle up.

That kind of cheating could be made a little harder, but not impossible, by keeping a daily log of IPs, and only increment the hits on uniques.

I'm sure no one in the contest would even dream of this kind of manipulation anyway.. well until now :matey:

Trev
05-06-2005, 01:41 PM
Originally posted by Mr Benn+May 6 2005, 06:38 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (Mr Benn @ May 6 2005, 06:38 PM)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-sarettah@May 6 2005, 09:25 AM
Landing page script.

http://www.bntnews.com/contest/rotateit.php

Heh. Good job i'm not in the contest with that as the landing page script ;)

With it set up how it is now, if you wanted to get nearly 100% of the traffic from it all you have to do is have a script on your server look for when a hit comes in, and as soon as it does, automatically send 3 hits of your own to cycle it round to your site again. Would just need some extra checks in there aswell to keep it in sync for when a real hit arrives in the middle of your generated hits and messes the cycle up.

That kind of cheating could be made a little harder, but not impossible, by keeping a daily log of IPs, and only increment the hits on uniques.

I'm sure no one in the contest would even dream of this kind of manipulation anyway.. well until now :matey: [/b][/quote]
I think I love you :okthumb: :wnw:

sarettah
05-06-2005, 01:51 PM
Originally posted by Mr Benn+May 6 2005, 12:38 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (Mr Benn @ May 6 2005, 12:38 PM)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-sarettah@May 6 2005, 09:25 AM
Landing page script.

http://www.bntnews.com/contest/rotateit.php

Heh. Good job i'm not in the contest with that as the landing page script ;)

With it set up how it is now, if you wanted to get nearly 100% of the traffic from it all you have to do is have a script on your server look for when a hit comes in, and as soon as it does, automatically send 3 hits of your own to cycle it round to your site again. Would just need some extra checks in there aswell to keep it in sync for when a real hit arrives in the middle of your generated hits and messes the cycle up.

That kind of cheating could be made a little harder, but not impossible, by keeping a daily log of IPs, and only increment the hits on uniques.

I'm sure no one in the contest would even dream of this kind of manipulation anyway.. well until now :matey: [/b][/quote]
That is the edited version of the script :)

I am hoping to hand it off to an uninvolved third party for implementation.

Plus there are checks against where the hits are coming from.

I have some other ideas on how to lock it down a bit tighter but some of it will be simply that the teams will not know the actual url of the landing page script only the guy providing the hits will.

sarettah
05-06-2005, 02:00 PM
Originally posted by Mr Benn+May 6 2005, 12:38 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td>QUOTE (Mr Benn @ May 6 2005, 12:38 PM)</td></tr><tr><td id='QUOTE'> <!--QuoteBegin-sarettah@May 6 2005, 09:25 AM
Landing page script.

http://www.bntnews.com/contest/rotateit.php

Heh. Good job i'm not in the contest with that as the landing page script ;)

With it set up how it is now, if you wanted to get nearly 100% of the traffic from it all you have to do is have a script on your server look for when a hit comes in, and as soon as it does, automatically send 3 hits of your own to cycle it round to your site again. Would just need some extra checks in there aswell to keep it in sync for when a real hit arrives in the middle of your generated hits and messes the cycle up.

That kind of cheating could be made a little harder, but not impossible, by keeping a daily log of IPs, and only increment the hits on uniques.

I'm sure no one in the contest would even dream of this kind of manipulation anyway.. well until now :matey: [/b][/quote]
Hmm... I'm sorry I'm stupid and didn't notice... But it looked to me like you were volunteering to help ?

:yowsa:

Mr Benn
05-06-2005, 02:01 PM
Ahhh :) Yup there's not much you can do if you're checking the referring URL and nobody knows what it is, or where the landing page is.

Mr Benn
05-06-2005, 02:03 PM
Originally posted by sarettah@May 6 2005, 10:01 AM
Hmm... I'm sorry I'm stupid and didn't notice... But it looked to me like you were volunteering to help ?

:yowsa:
I wasn't, but could do. Sounds like you've got it sussed anyway - i didn't realise you had omitted the security checks from the posted script :)

Trev
05-06-2005, 02:34 PM
Bugger :(

sarettah
05-06-2005, 03:27 PM
A more accurate depiction of what the teams might see is here http://www.bntnews.com/testit.html

or http://www.bntnews.com/landingpage/landingpage.php

If you click the link, you should see the teams url show in the address bar and you should see the referer printed out.

So, the team will see the referer string from wherever the hit provider is sending from (since this is a mix of TGP and SE hits I would expect to see TGP and SE referers) Once we know the domains that the contestants are using then they will be killed if the script recievs them as a referer.

So, without the team knowing the url of the landingpage and only seeing the original referer string I don't think they can do much to cheat that part of it (although, yes tech gurus probably could figure a way around it, I am figuring the tech skills of the contestants as part of the overall security formula :yowsa: )

The example script I posted was pretty much just to show the rotation aspect. I was hoping someone who knows more about php mysql would know perhaps a more efficient way to pull it off :)


Mr Benn. I might take you up on the volunteer aspect :)

Inabon volunteered Boldy, but I have not been able to grab the time to get back with them so I don't know what his time between now and next wednesday looks like :)

The second script is a touch more complicated (not much but a little). It will be capturing hits passed back from the teams and recycling them. So, it will have to capture a return hit and pass it to another team who is owed a hit.

I haven't decided what to do when no team is owed a hit yet. It will probably go to one of my domains or pass it to Nick :)

But the base structure is a mysql table with team, hitsin and hitsout defined.

It will get the hit, write hitsin to the appropriate team record and then grab the next team that is owed a hit and pass it on to them.

sarettah
05-06-2005, 05:11 PM
Ok, got the trade script more or less worked out.

Each team will send to a url with their teamname as a query string and the script witll count the hits coming in and dole out the hits going out accordingly :)

test of it is at http://www.bntnews.com/testtrade.html

Should always return a url belonging to a different team..

Need to come up with a way to tag a hit so I know when a hit has been passed between all 4 teams so we can ditch it to a default tade script and pick up a new hit :)