从数据库获取链接并重定向

时间:2017-03-25 16:18:39

标签: php mysql redirect

我试着计算链接被点击的次数,然后重定向到具有该ID的联盟网站。我已经设法做了点击部分,每次点击链接时它在数据库中更新1但是有重定向部分的问题,我将id发送到track.php然后在那里检索id但我从那里重定向有问题。在数据库中我有affiliate_link并存储在那里的网址。任何帮助都会很棒。

on track.php

<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/market/core/init.php'; 

//update count click in database
if(isset($_GET['id'])){
$id = is_numeric($_GET['id']);
$db->query("UPDATE credit_card_offers SET count_click = count_click + 1 WHERE id = '$id'");
header('Location: How_to_call_Affiliate_link_here_form_database?');
}
?>

on index.php。

<a href="track.php?id=<?= $product['id']; ?>">See Deal</a>

sql dump

-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 25, 2017 at 03:54 PM
-- Server version: 5.7.14
-- PHP Version: 5.6.25

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `dataz`
--

-- --------------------------------------------------------

--
-- Table structure for table `credit_card_offers`
--

CREATE TABLE `credit_card_offers` (
`id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`brand` int(11) NOT NULL,
`categories` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`balance_transfers` text COLLATE utf8_unicode_ci NOT NULL,
`purchases` text COLLATE utf8_unicode_ci NOT NULL,
`representative_apr` text COLLATE utf8_unicode_ci NOT NULL,
`representative_example` text COLLATE utf8_unicode_ci NOT NULL,
`affiliate_link` text COLLATE utf8_unicode_ci NOT NULL,
`featured` tinyint(4) NOT NULL DEFAULT '0',
`count_click` int(255) NOT NULL DEFAULT '0',
 `deleted` tinyint(4) NOT NULL DEFAULT '0'
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

 --
 -- Dumping data for table `credit_card_offers`
 --

INSERT INTO `credit_card_offers` (`id`, `title`, `brand`, `categories`, `image`, `description`, `balance_transfers`, `purchases`, `representative_apr`, `representative_example`, `affiliate_link`, `featured`, `count_click`, `deleted`) VALUES
(15, 'Barclaycard Platinum With Balance Transfer (25/25 Card)', 19, '19', '/market/images/products/5ac13b939568b60b71eb9fb1fa4d82df.png', 'A 2.5% fee is applied to balance transfers but a refund reduces this to 1.49% within 2 days (T&amp;Cs apply). Transfer a balance within 60 days of opening an account to get the 0% deal; otherwise the rate will be 18.9% p.a. variable with no fee. ', '0% for 25 months with a 1.49% fee', '0% for 25 months', '18.9% APR', 'Representative Example: The standard interest rate on purchases is 18.9% p.a. (variable), so if you borrow &pound;1,200 the Representative APR will be 18.9% APR (variable). ', 'https://www.barclaycard.co.uk/personal/platinum', 0, 0, 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `credit_card_offers`
--
ALTER TABLE `credit_card_offers`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `credit_card_offers`
--
ALTER TABLE `credit_card_offers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

1 个答案:

答案 0 :(得分:1)

track.php上,您需要使用其他查询来提取相关记录。另一种方法是在链接中包含URL,因为数据已在父页面上可访问。在index.php上执行:

<a href="track.php?id=<?= $product['id']; ?>&url=<?= urlencode($product['affiliate_link']);?>">See Deal</a>

然后在track.php上,您将其作为

$_GET['url']

然后你可以这样做:

if(!empty($_GET['url'])) {
     header('Location: ' . $_GET['url']);
     exit;
}

另请注意is_numeric返回一个布尔值,以便$id赋值也可能不正确。

您还可以验证网址http://php.net/manual/en/function.filter-var.phpFILTER_VALIDATE_URL