在PHP

时间:2017-05-16 20:16:40

标签: php .htaccess

我正在尝试创建像http://example.com/ref/referrer这样的联盟网址。

我有2个php文件和一个.htaccess文件。我有一个名为'referral'的数据库表,它在一个名为'referrer'的字段中存储用户的用户名。

的index.php

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$query = "SELECT referrer FROM referral";
$result = $conn->query($query);
$ref=mysqli_fetch_array($result);
$referrer=$ref['referrer'];

//Create referral URL
$refurl="http://example.com/ref/$referrer";
?>

welcome.php (通过参考链接访问网站的用户的目标网页)。

<?php
$referrer=mysqli_real_escape_string($con,$_GET["referrer"]);
$counted = "clicks"; 
//select user
$query="SELECT * FROM referral WHERE referrer='$referrer'";
$result=mysqli_query($con,$query) or die('error');
$user_ref=mysqli_fetch_array($result);
$user=$user_ref['user'];

if(!isset($_COOKIE[$counted])) { 

    //cookie for visitor
    setcookie("clicks", $counted, time() + (86400 * 7), "/refer/"); //7days

    //page view Counts
    $sql = "UPDATE referral SET clicks=clicks+1 WHERE referrer='$referrer'";
    $result_set=mysqli_query($con, $sql);
}
if($user_ref == false) {
    echo 'Sorry, the user does not exist.';
} else {  
    echo You were referred by $user;  
?>

.htaccess文件

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ welcome.php?referrer=$1

问题是引荐网址不起作用。单击时,它应该加载欢迎页面,但它不会。请有人能告诉我我做得不对或丢失了吗?

3 个答案:

答案 0 :(得分:0)

看起来它是你的mod-rewrite语句。 ^([a-zA-Z0-9_-]+)$无法与/ref/myrefcode01匹配。您可以尝试使用^ref/([a-zA-Z0-9_-]+)$

答案 1 :(得分:0)

将网址加载到字符串变量中不会重定向make!

您需要使用> random.boolean.dataframe$AND.rows [1] 0 0 1 函数调用进行重定向

header()

答案 2 :(得分:0)

根据@RiggsFolly和@UTAlan的建议,为了其他可能需要相同解决方案的人,我采取了以下措施。

在您的数据库中运行以下查询

os.path.join(BASE_DIR, 'templates')

<强> db.php中

CREATE TABLE IF NOT EXISTS `referral` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` varchar(500) NOT NULL,
  `referrer` varchar(500) NOT NULL,
  `clicks` int(11) DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user` (`user`,`referrer`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

<强>的index.php

<?php
session_start();
error_reporting(0);
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'password';
$dbname = 'my_database';

$con=mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) //connect to the database server
or die ("Could not connect to mysql because ".mysqli_error());
mysqli_select_db($con,$dbname) //select the database
or die ("Could not select to mysql because ".mysqli_error());
?>

welcome.php //通过推介链接来到网站的人的着陆页

<?php
include('db.php');
$conn = new mysqli($servername, $username, $password, $dbname);
$query = "SELECT referrer FROM referral";
$result = $conn->query($query);
$ref=mysqli_fetch_array($result);
$referrer=$ref['referrer'];

//Create referral URL
$refurl="http://example.com/ref/$referrer";
//redirect to welcome page
header("Location: $refurl");
}
?>

.htaccess文件

<?php
include('db.php');
$referrer=mysqli_real_escape_string($con,$_GET["referrer"]);
$counted = "clicks"; 
//select user
$query="SELECT * FROM referral WHERE referrer='$referrer'";
$result=mysqli_query($con,$query) or die('error');
$user_ref=mysqli_fetch_array($result);
$user=$user_ref['user'];

if(!isset($_COOKIE[$counted])) { 

    //cookie for visitor
    setcookie("clicks", $counted, time() + (86400 * 7), "/ref/"); //7days

    //page view Counts
    $sql = "UPDATE referral SET clicks=clicks+1 WHERE referrer='$referrer'";
    $result_set=mysqli_query($con, $sql);
}
if($user_ref == false) {
    echo 'Sorry, the user does not exist.';
} else {  
    echo 'You were referred by $user';  
?>

感谢AllWebTuts(http://allwebtuts.com)的Santhosh Veer对他的见解。