Jump to content

Only shows on second refresh..?


Go to solution Solved by oz11,

Recommended Posts

Well.. basically just wrote this. But want it to be forwarded to a page if it's complete... However, it only loads the final conditional if the page is refreshed a second time..

Help, please.

Code:

<?php
include 'config.php';
$user_ip=$_SERVER['REMOTE_ADDR'];
$news_ref = $_GET['ref'];
$stmt = $pdo->prepare("SELECT count(*) FROM outgoing_count WHERE IP = ? AND news_reff = ?");
$stmt->execute([$user_ip, $news_ref ]);
$count = $stmt->fetchColumn();

if($count == 0){
	$sql = "INSERT INTO outgoing_count (IP, news_reff) VALUES (?,?)";
	$stmt= $pdo->prepare($sql);
	$stmt->execute([$user_ip, $news_ref ]);
} else {
	$stmt = $pdo->prepare("SELECT count(*) FROM news WHERE reference=?");
	$stmt->execute([$_GET['ref']]);
	$check_exist = $stmt->fetchColumn();
	if($check_exist == 0){
		echo "404 Error: not found";
	} else {
		echo "Redirect. This page has been viewed ".$count."times"; // <--- ONLY SHOWS ON SECOND REFRESH..??...
		//header('Location: http://google.com');
	}
}


?>
Link to comment
https://forums.phpfreaks.com/topic/317082-only-shows-on-second-refresh/
Share on other sites

Updated the code a bit... still doesn't work..

<?php
include 'config.php';
$stmt = $pdo->prepare("SELECT count(*) FROM news WHERE reference=?");
$stmt->execute([$_GET['ref']]);
$check_exist = $stmt->fetchColumn();
if($check_exist == 0){
	echo "error not found";
	exit();
}

$user_ip=$_SERVER['REMOTE_ADDR'];
$stmt = $pdo->prepare("SELECT count(*) FROM outgoing_count WHERE IP = ? AND news_reff = ?");
$stmt->execute([$user_ip, $_GET['ref']]);
$count = $stmt->fetchColumn();

if($count == 0){
	$sql = "INSERT INTO outgoing_count (IP, news_reff) VALUES (?,?)";
	$stmt= $pdo->prepare($sql);
	$stmt->execute([$user_ip, $_GET['ref']]);
} else {
	echo "Redirect. This page has been viewed ".$count."times";
	//header('Location: http://google.com');
}
?>
Edited by oz11
  • Solution

Fixed code:

<?php
include 'config.php';
$stmt = $pdo->prepare("SELECT count(*) FROM news WHERE reference=?");
$stmt->execute([$_GET['ref']]);
$check_exist = $stmt->fetchColumn();
if($check_exist == 0){
	echo "error not found";
	exit();
}

$user_ip=$_SERVER['REMOTE_ADDR'];
$stmt = $pdo->prepare("SELECT count(*) FROM outgoing_count WHERE IP = ? AND news_reff = ?");
$stmt->execute([$user_ip, $_GET['ref']]);
$count = $stmt->fetchColumn();

if($count == 0){
	$sql = "INSERT INTO outgoing_count (IP, news_reff) VALUES (?,?)";
	$stmt= $pdo->prepare($sql);
	$stmt->execute([$user_ip, $_GET['ref']]);

	$stmt = $pdo->prepare("SELECT count(*) FROM outgoing_count WHERE news_reff = ?");
	$stmt->execute([ $_GET['ref']]);
	$count2 = $stmt->fetchColumn();
	echo "Redirect. This page has been viewed ".$count2."times";

} else {
	$stmt = $pdo->prepare("SELECT count(*) FROM outgoing_count WHERE news_reff = ?");
	$stmt->execute([$_GET['ref']]);
	$count3 = $stmt->fetchColumn();
	echo "Redirect. This page has been viewed ".$count3."times";
	//header('Location: http://google.com');
}
?>

.. just needed time. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.