AB.php

Gesponsorde koppelingen

PHP script bestanden

  1. index.php
  2. AB.php

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
  if(!isset($_SESSION)) {
    session_start();
  }

  /**
   * Singleton AB class
   */

  final class AB
  {
    private $abSelectFunction = "abSelectDefault";
    /**
     * Call this method to get singleton
     *
     * @return UserFactory
     */

    public static function Instance()
    {

      static $inst = null; // PHP needs this to be set here.
      if ($inst === null) {
        $inst = new AB();
      }

      return $inst;
    }


    /**
     * getAb returns the A/B test name.
     * The test selection is determined by the abSelect function.
     * You can provide your own abTest function by creating a globally accessible
     * function and pass the name of that function to the setAbSelectFunction.
     * @return the A/B test name.
     */

    public function getAb() {
      $ab = "default";
      if(strlen($this->abSelectFunction) > 0) {
        $ini_array = parse_ini_file(dirname(__FILE__) . "/AB.ini", true);
        $func = $this->abSelectFunction;
        $ab = $func($ini_array["Sites"]);
      }

      return $ab;
    }

    
    /**
     * setAbSelectFunction can be used to set your own
     * A/B test selection function.
     * @param $functionName name of your function which does the selecting.
     */

    public function setAbSelectFunction($functionName) {
      $this->abSelectFunction = $functionName;
    }

    
    /**
     * getAbSelectFunction returns the name of the A/B test selection function.
     * @return name of the A/B test selection function.
     */

    public function getAbSelectFunction() {
      return $this->abSelectFunction;
    }

    
    /**
     * Private constructor
     */

    private function __construct()
    {

      // Use Instance()
    }
    
    /**
     * Private clone
     */

    private function __clone()
    {

      // Me not like clones! Me smash clones!
    }
  }

  
  /**
   * abSelectDefault generates a random outcome with the given input.
   * The input is an array.
   * The output is an element from that array.
   * The test is stored in as a session variable and is reused during the session lifetime.
   */

  function abSelectDefault($array) {
    $ab = $array[mt_rand(0, (sizeof($array)-1))];
    
    // Check if AB cookie has been set. If set then it overrides the session value.
    // otherwise we use the session value if that has been set.

    if(isset($_COOKIE["ab"]) && strlen($_COOKIE["ab"]) > 0) {
      $_SESSION["ab"] = $_COOKIE["ab"];
      $ab = $_SESSION["ab"];
    }

    else if(isset($_SESSION["ab"]) && strlen($_SESSION["ab"]) > 0) {
      $ab = $_SESSION["ab"];
    }

    // Check for manual A/B test passed by URI. Don't allow path traversal characters.
    if(isset($_REQUEST["ab"]) && strlen($_REQUEST["ab"]) > 0 && preg_match("/(\.|\/)/", $_REQUEST["ab"]) == false) {
      $ab = $_REQUEST["ab"];
    }

    $_SESSION["ab"] = $ab;
    setcookie("ab", $_SESSION["ab"], time()+(10 * 365 * 24 * 3600));
    return $ab;
  }

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.