PHP投票系統+會員登入功能

unauna
edited 六月 2014 in PHP新手區

index.php↓
<?php
	$is_date = date("Y-m-d");
	time_Rang($is_date);
	function time_Rang($is_date){
	$user_ts = strtotime($is_date);
	
	if (($user_ts >= strtotime('2014-06-23')) && ($user_ts < strtotime('2014-06-30'))){
		include 'db_con.inc';
		$query="SELECT t_name FROM ex4_subject WHERE t_index = 2";
		$result = mysqli_query($link, $query);
		if ($result && mysqli_num_rows($result) > 0){
			$row = mysqli_fetch_row($result);
			$subject = $row[0];
		}else{
			echo "no data";
			goto end;
		}
		mysqli_free_result($result);
		$query = "SELECT SUM(t_count) FROM ex4_option WHERE s_index = 2 ";
		$result = mysqli_query($link, $query);
		if($result && mysqli_num_rows($result)>0){
			$row = mysqli_fetch_row($result);
			$total = $row[0];
		}else{
			echo "no data3";
			goto end;
		}
		$query = "SELECT * FROM ex4_option WHERE s_index =2";
		$result = mysqli_query($link, $query);
		if($result && mysqli_num_rows($result)>0){
?>
		<p><?php echo $subject;?></p>
			<form method="post" action="vote.php">
			<table width = "500">
			<?php 
				while($row = mysqli_fetch_assoc($result)){
					$per = @floor(($row['t_count']/$total)*100)."%";			
			?>
			<tr>
				<td width="30" align="center"><input type="radio" name="t_index" value="<?php echo $row["t_index"];?>"/></td>
				<td width="300"><?php echo $row["t_name"]; ?></td>
				<td width="100"><span class="bar" style="width:<?php echo $per;?>;"></span></td>
				<td width="50" align="center"><?php echo $per;?></td>
				<td width="120" align="center"><?php echo $row["t_count"];?></td>
			</tr>
			<?php 
				}
			?>
			</table>
			<input name="s_index" type="hidden" value="<?php echo $_GET["s_index"]; ?>"/>
			<p><input type='submit' value='投票'></p>
			</form>
<?php			
			}end:
				@mysqli_free_result($result);
				mysqli_close($link);
		}

vote.php↓
<?php
	include "db_con.inc";
	//$vote = "";
	include "ip_check.inc";

if (isset($_POST["t_index"]) == false){
	echo "<script>alert('沒有選項');</script>";
}
else if (isset($_POST["s_index"]) == true && $vote == true){
		 $query = "UPDATE ex4_option SET t_count=t_count+1 WHERE t_index = {$_POST['t_index']}";
		 mysqli_query($link, $query);
		 
		 $query = "INSERT INTO ex4_user (s_index,t_ip,t_time)VALUES
					('{$_POST['s_index']}', '{$_SERVER['REMOTE_ADDR']}',NOW())";
		 mysqli_query($link, $query);
		 mysqli_close($link);
		 echo "<script>alert('票數已計入');</script>";
}
else{
		 echo "<script>alert('{$TIME_LIMIT}分鐘內不能再投票');</script>";
}
	echo "<script>location.href = 'index.php';</script>";

?>

ip_check.php↓
<?php
	$TIME_LIMIT =1;
	$query = "DELETE FROM ex4_user WHERE t_time < NOW()- INTERVAL $TIME_LIMIT MINUTE";
	$result = mysqli_query($link, $query);//整理表單 如果本時間 小於 現在時間減掉60 就可以刪除

	$query = "SELECT * FROM ex4_user WHERE t_index
				AND t_ip = '{$_SERVER['REMOTE_ADDR']}'
				AND t_time > NOW()-INTERVAL $TIME_LIMIT MINUTE";
	$result = mysqli_query($link, $query);
	
	if($result && mysqli_num_rows($result) > 0){
		$vote = false;
	}else{
		$vote = true;
	}
	@mysqli_free_result($result);

?>


想請問說,如果我想要加入"登入後才能投票"的機制話;該怎麼寫?

ex.當使用者選擇了選項且按下了投票,這時"跳出請先登入後再投票"(前提是使用者沒有登入會員)。

煩請各位高手指點迷津

ORZ

謝謝!!

評論

Sign In or Register to comment.