Author Topic: Php Script I Need Written  (Read 1648 times)

Offline thablkpanda

  • Full Member
  • ***
  • Posts: 249
Php Script I Need Written
« on: June 26, 2006, 02:07:10 AM »
Yo,

Friends, acquantinces - etc.

I've spent the last few days trying to deciper old handwriting and rip apart my old code trying to create the following- but now I'll enlist one of you to do the work for me.. lol

When I say enlist; I mean someone that wants a challenge, or if you don't consider this coding bit difficult, then it should be a snap.

I need a script that will randomize my avatar and signature; but they need to match; like my green and white Tony Sinclair (Tanguray commercial guy) signature and avatar.

Thing is, I don't know how to save the variable I'm going to randomly generate (say the number 9) that will determine which avatar and signature set to call; since the  avatar and signature are called in different requests to the server when the server parses the forum page document thinger.

Any help from you master coders is greatly appreciated.

Thanks guys,
Chris

I need help with this

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Php Script I Need Written
« Reply #1 on: June 26, 2006, 07:39:31 AM »
You *could* use a cookie, or the PHP session API's.. just do this in both scripts.

Code: [Select]
<?php
session_start();

if (!isset($_SESSION["number"]))
{
  $_SESSION["number"] = rand()*10; // or whatever
}
else
{
  // produce the correct image for the number here.
}

?>

It might be easier to put both scripts into one, and then use $_SERVER["PATH_INFO"] or something similar to that.

(You can't use query_string since the forums will block out the ? character and think it points to an automated script).

Path info works like this:
http://www.yourdomain.com/scriptname.php/path_info_is_here

For example:
Code: [Select]
if ($_SERVER["PATH_INFO"] == "avatar.png")
{
  // avatar generation
}
else if ($_SERVER["PATH_INFO"] == "sig.png")
{
  // sig generation
}

(The PNG's are there to make the forums think you linked to a static png file).

Then, the last thing to remember is that you need to destroy the session
Code: [Select]
session_destroy();
(if you want them to randomize again on a new page request).

There might be a more ingenious way of doing it.. a cookie is the best way I can think of at the moment.
« Last Edit: June 26, 2006, 07:40:32 AM by op2hacker »

Offline thablkpanda

  • Full Member
  • ***
  • Posts: 249
Php Script I Need Written
« Reply #2 on: June 26, 2006, 03:28:55 PM »
See.

I think I understand that, my problem is getting it so both the images match. I think.

See- look. I've got the signature in BBCode saved to http://blahbla/sig.png

And the index.php in that directory is a randomziation script, that selects one image in that directory to show.

I need to save a random variable that indicates which avatar and signature SET to pull.

Yours just does randomization I think...

But you did give me an idea; cookies.

-Chris

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Php Script I Need Written
« Reply #3 on: June 26, 2006, 04:40:47 PM »
Well, the $_SESSION array in PHP, after you call session_start() is saved on the webserver. A cookie is sent to the browser with a session ID (this is so the server can look up the session data associated with that ID).

So essentially all the data in $_SESSION is cookied.

So if you call session_start() at the top of every page (btw.. this must be called before any output, it modifies page headers). the $_SESSION array will persist itself until the timeout expires (there is a function to set the timeout in minutes) or you call session_destroy().

I also have another idea, which is a lot simpler.

You could use $_SERVER["HTTP_REFERER"] and use the referring page (in the case of images, its the URL of the page which contained the <img> tag). That way when the user goes to a different page you could use the referrer to distinguish between visits.
This is how web hit counters do it so it doesn't count for each and every page load.

You could also use timers or counters to make sure that every other load, it gets changed for the IP visiting.

There isn't really a perfect solution for this.

Offline thablkpanda

  • Full Member
  • ***
  • Posts: 249
Php Script I Need Written
« Reply #4 on: June 26, 2006, 07:10:25 PM »
I noticed there's no perfect fix.

But you did give me some GREAT Ideas, thanks a ton.

I'm really not JUST making this for my signature and avatar rotation, If it works for that; It'll work for a more advanced project I'm doing in my free time - very sexy stuff.

I can't let it out in the open yet tho-

-Chris