使用作业 ID 命中计数器 [英] Hit counter using job id

查看:83
本文介绍了使用作业 ID 命中计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示访问者使用从 MySQL 数据库调用的 ID 查看工作的次数.

下面是页面job.details.php中的代码.

<div style="float:left; width:200px;"class="ara-form"><header style="font-size:12px; color:#666666; font:Arial, sans-serif; padding:7px;"><?php$result = mysqli_query($conn,"SELECT * FROM job WHERE jobid = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");$jobdetails = mysqli_fetch_assoc($result);echo '<strong>职位</strong></br>'.$jobdetails['positiontitle'].'
';echo '<strong>公司名称</strong></br>'.$jobdetails['companyname'].'
';echo '<strong>位置</strong></br>'.$jobdetails['location'].'
';echo '<strong>截止日期</strong></br>'.$jobdetails['closeddate'].'<hr class="job">';echo '<strong>空缺数量</strong></br>'.$jobdetails['numberofvacancy'].'
';echo '<strong>工作类别</strong></br>'.$jobdetails['jobcategory'].'
';echo '<strong>持续时间</strong></br>'.$jobdetails['duration'].'
';echo '<strong>就业类型</strong></br>'.$jobdetails['employmenttype'].'
';echo '<strong>薪水</strong></br>'.$jobdetails['salary'].'
';echo '<strong>计时</strong></br>'.$jobdetails['timing'].'
';echo '<strong>国籍</strong></br>'.$jobdetails['nationality'].'
';echo '<strong>性别</strong></br>'.$jobdetails['gender'].'
';echo '<strong>经验</strong></br>'.$jobdetails['experience'].'
';echo '<strong>教育</strong></br>'.$jobdetails['education'].'
';echo '<strong>性别</strong></br>'.$jobdetails['gender'].'
';echo '<strong>性别</strong></br>'.$jobdetails['gender'].'
';?></header></div><div style="float:left; width:600px;"class="ara-form"><fieldset style="font-size:12px; color:#666666; font:Arial, sans-serif;"><?phpecho '<p><strong id="ara-form">公司背景</strong></br>'.$jobdetails['background'].'</p></br>';echo '<p><strong id="ara-form">工作摘要</strong></br>'.$jobdetails['summary'].'</p></br>';echo '<p><strong id="ara-form">工作职责</strong></br>'.$jobdetails['duty'].'</p></br>';echo '<p><strong id="ara-form">资格</strong></br>'.$jobdetails['资格'].'</p></br>';echo '<p><strong id="ara-form">技能</strong></br>'.$jobdetails['skill'].'<hr class="job"></p></br>';echo '<p><strong id="ara-form">提交指南</strong></br>'.$jobdetails['submission'].'</p></br>';echo '<p><strong id="ara-form">搜索这个职位的词</strong></br>'.$jobdetails['search'].'</p></br>';?></fieldset></div><div style="float:left; width:33.4%; background:#ccc;">三</div><div style="clear:both;"></div>

这是我从 MySQL 中通过其 ID 调用的作业示例:http://example.com/job/job.details.php?id=171

解决方案

正如@I can Has Kittenz 所说,它不是临时存储,需要持久化.我可以建议创建一个类似的表:

mysql>创建表`pageviews`(->`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,->`job_id` int,->`remote_host` varchar(45)->);

PHP 部分:

$ipaddress = $_SERVER['REMOTE_ADDR'];$job_id = $_GET['id'];/** 使用此远程主机检查此作业 ID 是否存在条目*/$query = "SELECT id FROM pageviews WHERE "."job_id=".$job_id." AND remote_host='".$ipaddress."'";$result = mysqli_query($dbc, $query);if(mysqli_num_rows($result) !== 1){/** 此 job_id 没有条目.* 所以创建一个条目*/$query = "INSERT INTO pageviews (job_id,remote_host) VALUES "."(".$job_id.",'".$ipaddress."')";$result = mysqli_query($dbc, $query);如果($结果){}别的{echo '插入数据时出现问题';出口();}}

如果上面的 PHP 部分插入到您的 job.details.php 中,它将查看客户端的 IP 地址、他/她正在访问的 JOB ID,然后在他/她正在访问的表中插入一个条目表条目中不存在的 IP 中的作业 ID.

现在,您可以使用简单的查询获得jobid: $_GET['id'] 被查看的次数":

$query = "select id from pageviews where job_id=".$job_id."";$result = mysqli_query($dbc, $query);echo "jobID: ".$job_id." 被查看的次数 = ".mysqli_num_rows($result);

注意:客户端 IP 可以位于代理服务器后面,或者 IP 可以是代理后面 LAN 的内部 IP.所以不能真正相信 $_SERVER['REMOTE_ADDR'] 提供的价值.

您可以自由地优化任何查询、清理输入、以不同的方式创建表.这只是实现您的基本目标的简单方法之一.

I want to show that how many times a job have been viewed by visitors using their id which have been called from MySQL database.

Below is the code in page job.details.php.

<div style="width:900px;">
    <div style="float:left; width:200px;"class="ara-form"><header style="font-size:12px; color:#666666; font:Arial, sans-serif; padding:7px;"><?php

   $result = mysqli_query($conn,"SELECT * FROM job WHERE jobid = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");

   $jobdetails = mysqli_fetch_assoc($result);

    echo '<strong>Job Title</strong></br> '.$jobdetails['positiontitle'].'<hr class="job">';
    echo '<strong>Company Name</strong></br> '.$jobdetails['companyname'].'<hr class="job">';
    echo '<strong>Location</strong></br> '.$jobdetails['location'].'<hr class="job">';
    echo '<strong>Closing Date</strong></br> '.$jobdetails['closingdate'].'<hr class="job">';
    echo '<strong>Number of Vacancy</strong></br> '.$jobdetails['numberofvacancy'].'<hr class="job">';
    echo '<strong>Job Category</strong></br> '.$jobdetails['jobcategory'].'<hr class="job">';
    echo '<strong>Duration</strong></br> '.$jobdetails['duration'].'<hr class="job">';
    echo '<strong>Employment Type</strong></br> '.$jobdetails['employmenttype'].'<hr class="job">';
    echo '<strong>Salary</strong></br> '.$jobdetails['salary'].'<hr class="job">';
    echo '<strong>Timing</strong></br> '.$jobdetails['timing'].'<hr class="job">';
    echo '<strong>Nationality</strong></br> '.$jobdetails['nationality'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';
    echo '<strong>Experience</strong></br> '.$jobdetails['experience'].'<hr class="job">';
    echo '<strong>Education</strong></br> '.$jobdetails['education'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';
    echo '<strong>Gender</strong></br> '.$jobdetails['gender'].'<hr class="job">';


?></header></div>
    <div style="float:left; width:600px;" class="ara-form">
    <fieldset style="font-size:12px; color:#666666; font:Arial, sans-serif;">
    <?php

    echo '<p><strong id="ara-form">Company Background</strong></br> '.$jobdetails['background'].'</p></br>';
    echo '<p><strong id="ara-form">Job Summary</strong></br> '.$jobdetails['summary'].'</p></br>';
    echo '<p><strong id="ara-form">Job Duties and Responsibilities</strong></br> '.$jobdetails['duty'].'</p></br>';
    echo '<p><strong id="ara-form">Qualification</strong></br> '.$jobdetails['qualification'].'</p></br>';
    echo '<p><strong id="ara-form">Skills</strong></br> '.$jobdetails['skill'].'<hr class="job"></p></br>';
    echo '<p><strong id="ara-form">Submission Guideline</strong></br> '.$jobdetails['submission'].'</p></br>';
    echo '<p><strong id="ara-form">Words to search this job</strong></br> '.$jobdetails['search'].'</p></br>';
    ?>
    </fieldset></div>
    <div style="float:left; width:33.4%; background:#ccc;">three</div>
    <div style="clear:both;"></div>
</div>

</div>

This is an example of the jobs that i have been called by their id from MySQL: http://example.com/job/job.details.php?id=171

解决方案

As said by @I can Has Kittenz, its not a temporary storage and needs persistence. I can suggest creating a table something like :

mysql> create table `pageviews`(
->  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
->  `job_id` int,
-> `remote_host` varchar(45)
-> );

PHP Part:

$ipaddress  =   $_SERVER['REMOTE_ADDR'];
$job_id     =   $_GET['id'];
/*
 * Checking if an entry exists for this JOB ID with this REMOTE HOST
 */
$query  =   "SELECT id FROM pageviews WHERE ".
                      "job_id=".$job_id." AND remote_host='".$ipaddress."'";
$result =   mysqli_query($dbc, $query);
if(mysqli_num_rows($result) !== 1){
/*
 * There is no entry for this job_id.
 * So create an entry
 */
    $query = "INSERT INTO pageviews (job_id,remote_host) VALUES ".
        "(".$job_id.",'".$ipaddress."')";
    $result = mysqli_query($dbc, $query);
    if($result){}
    else{
    echo 'There was a problem inserting the data';
    exit();
    }
} 

If the above PHP part is inserted in your job.details.php, it will look at the client's IP address, the JOB ID which he/she is accessing and then insert an entry into your table IF he/she is accessing the JOB ID from an IP which does not exist in your table entries.

Now, you can get the 'Number of times jobid: $_GET['id'] was viewed' using a simple query :

$query  = "select id from pageviews where job_id=".$job_id."";
$result = mysqli_query($dbc, $query);
echo "Number of times jobID: ".$job_id." was viewed = ".mysqli_num_rows($result);

NOTE: Client IP can be behind a proxy server, or the IP can be an internal IP from the LAN behind the proxy. So cannot really trust the value offered by $_SERVER['REMOTE_ADDR'].

You are free to optimize any queries,sanitize inputs, create table in different ways. This is just one of the simple ways to achieve what you are basically looking for.

这篇关于使用作业 ID 命中计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆