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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 从数据库获取链接并重定向脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试着计算链接被点击的次数,然后重定向到具有该ID的联盟网站.我已经设法做了点击部分,每次点击链接时它在数据库中更新1但是有重定向部分的问题,我将id发送到track.PHP然后在那里检索id但我从那里重定向有问题.在数据库我有affiliate_link并存储在那里的网址.任何帮助都会很棒.

在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?');
}
?>

在index.PHP上.

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

sql转储

-- 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;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);

--
-- 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 */;

解决方法

在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赋值也可能不正确.

您还可以验证URL,http://php.net/manual/en/function.filter-var.php和FILTER_VALIDATE_URL.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 从数据库获取链接并重定向全部内容,希望文章能够帮你解决php – 从数据库获取链接并重定向所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。