GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   All you clever coder types ! Beginners PHP Question (https://gfy.com/showthread.php?t=843439)

CurrentlySober 07-24-2008 07:29 AM

All you clever coder types ! Beginners PHP Question
 
Just starting 'learning' PHP...

Can a more experienced coder show me the way?

What Im looking to do, is to have a sig on a surfer forum that reads 'CLICK HERE! LUCKY DIP!'

They click the link it goes to one-of-my-domians.com/php-page.php

That php page has 3 or 5 different URLs programmed into it... ie domain-1.com/blog/ domain-2.com/sexygirls/index.html and domain-3.com etc etc...

When the sig clicking surfer hits the php page at one-of-my-domians.com/php-page.php it randomly chooses one of the 3 or 5 domains, and sends him to it... In other words, per click each domain has a 1 in 3 or 1 in 5 chance of getting the 'hit'...

I'm not interested in 'Weighting' the results... Im just trying to learn the simplest, cleanest way to use PHP to split the results 'randomly' ?

Whats the right PHP to do this a simple as poss?

Be gentle with me LOL I'm genuinely TRYING to learn, and we all have to start somewhere !

JayS 07-24-2008 07:32 AM

Quote:

Originally Posted by ThatGuyInTheCorner (Post 14499182)
Be gentle with me LOL I'm genuinely TRYING to learn, and we all have to start somewhere !

http://php.net/array_rand

mikeyddddd 07-24-2008 07:38 AM

I do it like this:

<?
$urls = file ("random.txt"); //replace with the name of your file
srand(time());
$count = count($urls);
$random = (rand()%$count);
Header("Location: $urls[$random]");
?>

random.txt contains a list of URLs.

You could put the URLs into an array in the code if you prefer. I have them in a separate file so I can use them from other code.

Example is in my sig.


gornyhuy 07-24-2008 08:01 AM

What mikey said, but if you don't wanna use a file for the urls, here's an easy way to do the arrray:

<?
$linkArray[0]="http://www.google.com";
$linkArray[1]="http://www.yahoo.com.com";
srand(time());
$count = count($linkArray);
$random = (rand()&#37;$count);
Header("Location: $linkArray[$random]");
?>

ScriptWorkz 07-24-2008 08:04 AM

Quote:

Originally Posted by mikeyddddd (Post 14499221)
I do it like this:

<?
$urls = file ("random.txt"); //replace with the name of your file
srand(time());
$count = count($urls);
$random = (rand()&#37;$count);
Header("Location: $urls[$random]");
?>

random.txt contains a list of URLs.

You could put the URLs into an array in the code if you prefer. I have them in a separate file so I can use them from other code.

Example is in my sig.


this is pretty much the best way to do it, personally i'd just make the code like this if you just need simple

PHP Code:

<?php
$urls 
file("./random.txt");
exit(
header("Location: " $urls[rand(0, (count($urls)-1))]));
?>


tranza 07-24-2008 10:00 AM

Man this is crazy!!

munki 07-24-2008 10:02 AM

You tricky script kiddies you...

plsureking 07-24-2008 10:13 AM

wanna sell those clicks? lol

BigBen 07-24-2008 11:43 AM

Code:

$urls = file('urls.txt');
header("Location: " . array_rand($urls));


d-null 07-24-2008 12:52 PM

Quote:

Originally Posted by tranza (Post 14500069)
Man this is crazy!!

I see nothing of the sort here :error

mrkris 07-24-2008 01:27 PM

I was just notified about this thread and thought I would come in with what I use across my huge network of highly redundant traffic nodes. Advanced eyes only.

Code:

<?php
$domain_base_url = 'http://www.yourdomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');

$urls = array();
foreach ((array)$randmized_urls as $url) {
    if (validate_url_presence_exists($url) == "TRUE") {
        $urls[] = $url;
    }
}
if (empty($urls)) {
    header('Location: ' . $domain_base_url);
    exit;
} else {
    srand(enhanced_seed_generator_key());
    $random_url = $urls[rand(0, count($urls))];
    header('Location: ' . $random_url);
    exit;
}

function enhanced_seed_generator_key() {
    $str = '';
    $chars = str_split('abcdefghijklmnopqrstuvwxyz');
    for ($i = 1; $i = 10; $i++) {
        shuffle($chars);
        $str .= $chars[rand(0, count($chars))];
    }   
    return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}

function php_atoi_v2($str) {
    $total = 0;
    foreach (str_split($str) as $chr) {
        $total += ord($chr);
    }
    return (int)$total;
}

function validate_url_presence_exists($url) {
    $ch = curl_init();
    curl_setopt($ch, curlOPT_URL, $url);
    curl_setopt($ch, curlOPT_NOBODY, true);
    curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, curlOPT_HEADER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    return false if $res === false;
    $code = explode($res);
    return $code[1] == '200' ? "TRUE" : "FALSE";
}
?>


GrouchyAdmin 07-24-2008 01:31 PM

See sig. :)

GrouchyAdmin 07-24-2008 02:00 PM

Quote:

Originally Posted by mrkris (Post 14501657)
I was just notified about this thread and thought I would come in with what I use across my huge network of highly redundant traffic nodes. Advanced eyes only.

Code:

<?php
$domain_base_url = 'http://www.yourdomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');

$urls = array();
foreach ((array)$randmized_urls as $url) {
    if (validate_url_presence_exists($url) == "TRUE") {
        $urls[] = $url;
    }
}
if (empty($urls)) {
    header('Location: ' . $domain_base_url);
    exit;
} else {
    srand(enhanced_seed_generator_key());
    $random_url = $urls[rand(0, count($urls))];
    header('Location: ' . $random_url);
    exit;
}

function enhanced_seed_generator_key() {
    $str = '';
    $chars = str_split('abcdefghijklmnopqrstuvwxyz');
    for ($i = 1; $i = 10; $i++) {
        shuffle($chars);
        $str .= $chars[rand(0, count($chars))];
    }   
    return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}

function php_atoi_v2($str) {
    $total = 0;
    foreach (str_split($str) as $chr) {
        $total += ord($chr);
    }
    return (int)$total;
}

function validate_url_presence_exists($url) {
    $ch = curl_init();
    curl_setopt($ch, curlOPT_URL, $url);
    curl_setopt($ch, curlOPT_NOBODY, true);
    curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, curlOPT_HEADER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    return false if $res === false;
    $code = explode($res);
    return $code[1] == '200' ? "TRUE" : "FALSE";
}
?>



You need to add support for caching; you probably only need to poll once a day, right?

Here, have some functions:

Code:

function read_cachefile($url = FALSE) {
  if (function_exists('file_get_contents')) {
    $array = file_get_contents($url);
  } else {
      $array = "";
      if (file_exists($url)) {
        $fp = fopen($url, 'r');
        while (!FEOF($fp)) {
          $array .= fread($fp, 4096);
        }
        fclose($fp);
      }
    }
    return ($array ? unserialize($array) : FALSE);
  }

  function beginDay($date = FALSE) {
    $date = (isset($date)) ? strtotime($date) : time();
    return date("U", mktime(0, 0, +1, date("m", $date), date("d", $date), date("Y", $date)));
  }

HOPE THAT HELPS

Sands 07-24-2008 02:08 PM

Quote:

Originally Posted by mrkris
<?php
$domain_base_url = 'http://www.yourdomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');

$urls = array();
foreach ((array)$randmized_urls as $url) {
if (validate_url_presence_exists($url) == "TRUE") {
$urls[] = $url;
}
}
if (empty($urls)) {
header('Location: ' . $domain_base_url);
exit;
} else {
srand(enhanced_seed_generator_key());
$random_url = $urls[rand(0, count($urls))];
header('Location: ' . $random_url);
exit;
}

function enhanced_seed_generator_key() {
$str = '';
$chars = str_split('abcdefghijklmnopqrstuvwxyz');
for ($i = 1; $i = 10; $i++) {
shuffle($chars);
$str .= $chars[rand(0, count($chars))];
}
return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}

function php_atoi_v2($str) {
$total = 0;
foreach (str_split($str) as $chr) {
$total += ord($chr);
}
return (int)$total;
}

function validate_url_presence_exists($url) {
$ch = curl_init();
curl_setopt($ch, curlOPT_URL, $url);
curl_setopt($ch, curlOPT_NOBODY, true);
curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
curl_setopt($ch, curlOPT_HEADER, true);
$res = curl_exec($ch);
curl_close($ch);
return false if $res === false;
$code = explode($res);
return $code[1] == '200' ? "TRUE" : "FALSE";
}
?>

Quote:

Originally Posted by GrouchyAdmin (Post 14501783)
You need to add support for caching; you probably only need to poll once a day, right?

Here, have some functions:

Code:

function read_cachefile($url = FALSE) {
  if (function_exists('file_get_contents')) {
    $array = file_get_contents($url);
  } else {
      $array = "";
      if (file_exists($url)) {
        $fp = fopen($url, 'r');
        while (!FEOF($fp)) {
          $array .= fread($fp, 4096);
        }
        fclose($fp);
      }
    }
    return ($array ? unserialize($array) : FALSE);
  }

  function beginDay($date = FALSE) {
    $date = (isset($date)) ? strtotime($date) : time();
    return date("U", mktime(0, 0, +1, date("m", $date), date("d", $date), date("Y", $date)));
  }

HOPE THAT HELPS

My nipples are hard.

mrkris 07-24-2008 02:19 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14501783)
You need to add support for caching; you probably only need to poll once a day, right?

...

Thanks. I took your code but modified it to work with memcache. ENJOY THE SCALABILITY.

Code:

<?php
### DEFINE MEMCACHE CONSTANTS
DEFINE('MEMCACHE_HOST', 'localhost');
DEFINE('MEMCACHE_PORT', 11211);

### THIS IS MY DOMAIN, PLZ DONT USE MY LINKS
$domain_base_url = 'http://www.mydomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');

$urls = array();
foreach ((array)$randomized_urls as $url) {
    if (!check_cache($url)) {
        ### MAKE SURE ITS ONLINE
        if (validate_url_presence_exists($url) == "TRUE") {
            $urls[] = $url;
            add_cache($url);
        }
    } else {
        $urls[] = $url;
    }
}
if (empty($urls)) {
    ### EVERYONE LIKES HEAD // todo, get head
    header('Location: ' . $domain_base_url);
    exit;
} else {
    srand(enhanced_seed_generator_key());
    $random_url = $urls[rand(0, count($urls)-1)]; // BUG FIXED
    header('Location: ' . $random_url);
    exit;
}

### PASS MY SEED
function enhanced_seed_generator_key() {
    $str = '';
    $chars = str_split('abcdefghijklmnopqrstuvwxyz');
    for ($i = 1; $i = 10; $i++) {
        shuffle($chars);
        $str .= $chars[rand(0, count($chars))];
    }   
    return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}

### PHP FAILURE
function php_atoi_v2($str) {
    $total = 0;
    foreach (str_split($str) as $chr) {
        $total += ord($chr);
    }
    return (int)$total;
}

function validate_url_presence_exists($url) {
    $ch = curl_init();
    curl_setopt($ch, curlOPT_URL, $url);
    curl_setopt($ch, curlOPT_NOBODY, true);
    curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, curlOPT_HEADER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    return false if $res === false;
    $code = explode($res);
    return $code[1] == '200' ? "TRUE" : "FALSE";
}

function check_cache($url) {
    $memcache = get_memcache_object_balanced();
    return $memcache->get($url);
}

function add_cache($url) {
    $memcache = get_memcache_object_balanced();
    $memcache->set($url, 1, 0, 86400);
}

### BALANCE THE LOAD??? TODO -- MAKE IT BALANCE
function get_memcache_object_balanced() {
    $memcache = new Memcache;
    $memcache->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ("Could not connect");
    return $memcache;   
}


dig420 07-24-2008 02:25 PM

ok which one of you guys knows nats and is looking for work? :)

mrkris 07-24-2008 02:34 PM

Quote:

Originally Posted by dig420 (Post 14501890)
ok which one of you guys knows nats and is looking for work? :)

The brotherhoods knowledge can not be purchased, unless you have money, and hookers.

GrouchyAdmin 07-24-2008 02:35 PM

Quote:

Originally Posted by mrkris (Post 14501961)
The brotherhoods knowledge can not be purchased, unless you have money, and hookers.

Are you trying to undercut me, bitch?! It's Money, Hookers, and Blow.

mrkris 07-24-2008 02:37 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14501979)
Are you trying to undercut me, bitch?! It's Money, Hookers, and Blow.

I AM THE PUMPKIN KING.

Bama 07-24-2008 02:39 PM

lol - you guys are going to be measuring dicks before this thread is through!

mrkris 07-24-2008 02:44 PM

Quote:

Originally Posted by Bama (Post 14502003)
lol - you guys are going to be measuring dicks before this thread is through!

We already do that in the backroom at the shows

Bro Media - BANNED FOR LIFE 07-24-2008 02:44 PM

wait, let me try this

PHP Code:

<?php
$outurls 
= array("http://site1.com""http://site2.com""http://site3.com""http://site4.com""http://site5.com""http://site6.com""http://site7.com");
$r rand(0count($outurls));
header("location: " $outurls[$r]);
?>

yay!!

testpie 07-24-2008 02:56 PM

Quote:

Originally Posted by mrkris (Post 14501866)
Thanks. I took your code but modified it to work with memcache. ENJOY THE SCALABILITY.

Code:

<?php
### DEFINE MEMCACHE CONSTANTS
DEFINE('MEMCACHE_HOST', 'localhost');
DEFINE('MEMCACHE_PORT', 11211);

### THIS IS MY DOMAIN, PLZ DONT USE MY LINKS
$domain_base_url = 'http://www.mydomain.com/';
$randomized_urls = file($domain_base_url . 'files/urls.txt');

$urls = array();
foreach ((array)$randomized_urls as $url) {
    if (!check_cache($url)) {
        ### MAKE SURE ITS ONLINE
        if (validate_url_presence_exists($url) == "TRUE") {
            $urls[] = $url;
            add_cache($url);
        }
    } else {
        $urls[] = $url;
    }
}
if (empty($urls)) {
    ### EVERYONE LIKES HEAD // todo, get head
    header('Location: ' . $domain_base_url);
    exit;
} else {
    srand(enhanced_seed_generator_key());
    $random_url = $urls[rand(0, count($urls)-1)]; // BUG FIXED
    header('Location: ' . $random_url);
    exit;
}

### PASS MY SEED
function enhanced_seed_generator_key() {
    $str = '';
    $chars = str_split('abcdefghijklmnopqrstuvwxyz');
    for ($i = 1; $i = 10; $i++) {
        shuffle($chars);
        $str .= $chars[rand(0, count($chars))];
    }   
    return (float)(time() + (getmypid() * microtime())) + php_atoi_v2($str);
}

### PHP FAILURE
function php_atoi_v2($str) {
    $total = 0;
    foreach (str_split($str) as $chr) {
        $total += ord($chr);
    }
    return (int)$total;
}

function validate_url_presence_exists($url) {
    $ch = curl_init();
    curl_setopt($ch, curlOPT_URL, $url);
    curl_setopt($ch, curlOPT_NOBODY, true);
    curl_setopt($ch, curlOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, curlOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, curlOPT_HEADER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    return false if $res === false;
    $code = explode($res);
    return $code[1] == '200' ? "TRUE" : "FALSE";
}

function check_cache($url) {
    $memcache = get_memcache_object_balanced();
    return $memcache->get($url);
}

function add_cache($url) {
    $memcache = get_memcache_object_balanced();
    $memcache->set($url, 1, 0, 86400);
}

### BALANCE THE LOAD??? TODO -- MAKE IT BALANCE
function get_memcache_object_balanced() {
    $memcache = new Memcache;
    $memcache->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ("Could not connect");
    return $memcache;   
}


And that's the reason my code now looks like this:
Code:

<?php
function nameHere() {
 die("Error: I can't code for shit");
}
?>


mrkris 07-24-2008 02:56 PM

Quote:

Originally Posted by Jaysin (Post 14502052)
wait, let me try this

PHP Code:

<?php
$outurls 
= array("http://site1.com""http://site2.com""http://site3.com""http://site4.com""http://site5.com""http://site6.com""http://site7.com");
$r rand(0count($outurls));
header("location: " $outurls[$r]);
?>

yay!!

Your code isn't optimized, so I'm not going to even give it a moment of my time. Sucker.

Bro Media - BANNED FOR LIFE 07-24-2008 03:01 PM

Quote:

Originally Posted by mrkris (Post 14502143)
Your code isn't optimized, so I'm not going to even give it a moment of my time. Sucker.

I'll optimize my weiner on your jaw!

GrouchyAdmin 07-24-2008 03:05 PM

Hey, assholes.

count() positive indexes starts at one (0 == FALSE (when not === tested)).
arrays start at zero.

Bro Media - BANNED FOR LIFE 07-24-2008 03:07 PM

Quote:

Originally Posted by GrouchyAdmin (Post 14502203)
Hey, assholes.

count() positive indexes starts at one (0 == FALSE (when not === tested)).
arrays start at zero.

stfu i rarely use count() and all that jazz, gawd its for n00bs like you and mrkris

mrkris 07-24-2008 03:07 PM

Quote:

Originally Posted by Jaysin (Post 14502171)
I'll optimize my weiner on your jaw!

your weiner isn't optimized enough to get hard.

Bro Media - BANNED FOR LIFE 07-24-2008 03:08 PM

n00bs, every last one of ya

mrkris 07-24-2008 03:10 PM

Quote:

Originally Posted by Jaysin (Post 14502215)
n00bs, every last one of ya

Don't hate, just learn to code. Is that to much to ask?

GrouchyAdmin 07-24-2008 03:11 PM

Quote:

Originally Posted by Jaysin (Post 14502215)
n00bs, every last one of ya

I'm not paying you to 'consult', I'm paying you to 'take three cocks in the rectal cavity'.

FOCUS, GOD DAMN IT!

Bro Media - BANNED FOR LIFE 07-24-2008 03:15 PM

:( :( :(

dig420 07-24-2008 03:25 PM

Quote:

Originally Posted by mrkris (Post 14501961)
The brotherhoods knowledge can not be purchased, unless you have money, and hookers.

I provide the money, what you do with it is your business ;)

Hit me up on icq if you or grouchy is interested, I need a coder who can handle more than two days of working before he needs a two week break.

gornyhuy 07-24-2008 03:26 PM

count($cockinches[$GrouchyAdmin]) starts and ENDS at zero.

Ooooh nerd burn.

testpie 07-24-2008 03:36 PM

Quote:

Originally Posted by Jaysin (Post 14502215)
n00bs, every last one of ya

I admit it. I'm not even pro enough to get those two zeros in the noob tag...

GrouchyAdmin 07-24-2008 03:37 PM

Quote:

Originally Posted by gornyhuy (Post 14502317)
count($cockinches[$GrouchyAdmin]) starts and ENDS at zero.

Ooooh nerd burn.

All you're gonna get is that $cockinches[$GrouchyAdmin] is not set. Not circumcised, either.

NickPapageorgio 07-24-2008 03:40 PM

<? echo "Hello World!!!"; ?>

You can thank me later. ;)

LazyD 07-24-2008 03:45 PM

You all fail at speed optimization with your over-engineered solutions :1orglaugh
Array solution is over 20 times faster than file cache solution which is already 3 times faster than memcache solution, so all of you :321GFY

Lane 07-24-2008 03:46 PM

switch to mt_rand already

also, seeding is mostly useless

Bro Media - BANNED FOR LIFE 07-24-2008 03:48 PM

Quote:

Originally Posted by Lane (Post 14502439)
switch to mt_rand already

also, seeding is mostly useless

Holy shit, I thought you were dead... any updates on CJUltra man? I've defended that script so many times because of people assuming it was sending visitors to warez sites and shit because of your skim... wooo, update it or something man.

GrouchyAdmin 07-24-2008 03:56 PM

Quote:

Originally Posted by LazyD (Post 14502437)
You all fail at speed optimization with your over-engineered solutions :1orglaugh
Array solution is over 20 times faster than file cache solution which is already 3 times faster than memcache solution, so all of you :321GFY

You were so damn close, too.

HighEnergy 07-24-2008 04:30 PM

Congratulations - 300 lines of code to rotate 5 links.

mrkris 07-24-2008 06:56 PM

Quote:

Originally Posted by HighEnergy (Post 14502711)
Congratulations - 300 lines of code to rotate 5 links

If you don't understand the awesomeness, you are in the wrong field.

CurrentlySober 07-31-2008 01:43 PM

Update ! Just revisiting this thread to say thanks for all the replies... Even if I DID get totally lost towards the end LOL

I went with the Mikeyddddd solution in the end. I liked the idea of the seperate text file...

You see, as opposed to just posting 'how do I do this?' and grabbing the first reply, copy pasting it, and going off on my merry way...

I do actually want to learn, so I went out a bought (DONT LAUGH) a 'book' ? and have been reading it over the last few days... Now at least I understand (kinda) HOW it works...

Anyway, I have it all working like a dream!
So THANKS :)

Bro Media - BANNED FOR LIFE 07-31-2008 01:45 PM

does it have a section on poop?

CurrentlySober 07-31-2008 01:49 PM

Quote:

Originally Posted by Jaysin (Post 14540617)
does it have a section on poop?


:)

Please note: When I'm posting serious threads, I post sig free :)

CurrentlySober 07-31-2008 01:53 PM

Quote:

Originally Posted by Jaysin (Post 14540617)
does it have a section on poop?

I have however, enjoyed reading the book while sat on the toilet, so there is a tenuous connection I suppose :)

mrkris 07-31-2008 01:58 PM

Quote:

Originally Posted by ThatGuyInTheCorner (Post 14540645)
I have however, enjoyed reading the book while sat on the toilet, so there is a tenuous connection I suppose :)

Your pooping is not optimized. You need to straighten your back to allow for maxium force.

GrouchyAdmin 07-31-2008 02:04 PM

Quote:

Originally Posted by mrkris (Post 14540669)
Your pooping is not optimized. You need to straighten your back to allow for maxium force.

The last time a noob tried that, he shit out his whole spine.


All times are GMT -7. The time now is 07:52 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123