Jump to content

PHP/SQL If no connection die


verdegallardo

Recommended Posts

Hi All,

 

I have a landing page that I use, but often my sql database is too busy or gets overwhelmed and then the landing page just stalls and traffic is lost.  Here it is:

 

<?php
//ini_set("display_errors",1);
if(!$src) $src = $q;
define("__EXEC_", 1);
include("config.php");
include("custom.php");
function lsp_visit_find($__val, &$__uid) {
global $u;
global $db;
$l_campaign = array("campaign_id"=>"", "url"=>"", "cost"=>"0.00", "name"=>"-", "ad_keyword"=>"", "mapped_keyword"=>"");
$l_visit = array();
$q = $_GET['q'];
$src = $_GET['src'];
// for STAGE #1 (visit definition): get cost, ad keyword, mapped keyword.
$l_campaign['name'] = "info";
$l_campaign['mapped_keyword'] = "$q";
  $l_campaign['ad_keyword'] = "$src";
  $l_campaign['cost'] = ".02";

// get referer (campaign URL). if no campaign found, - create it!
if (!empty($l_campaign['name'])) {
	$r = mysql_query("select campaign_id,name from campaign where name='$l_campaign[name]'");
	if (!mysql_affected_rows($db)) {
		$r = mysql_query("insert into campaign(name,cost, creation_date,last_update_date) values('$l_campaign[name]',$l_campaign[cost], unix_timestamp(),unix_timestamp())");
		$l_campaign['campaign_id'] = mysql_insert_id($db);
	}
	else {

		$w = mysql_fetch_assoc($r);
		$l_campaign = array_merge($l_campaign,$w);
	}
}
// check, whether we had visits from this IP for this keywords within 2 minutes.
// if we have one, get its ID, otherwise - create new!
$visit_id="";
if (!empty($l_campaign['ad_keyword']) && !empty($l_campaign['mapped_keyword']) ) {
	$ip = get_real_ip();
	$r = mysql_query("select visit_id,uid,campaign_id,ad_keyword,mapped_keyword from visit where ip='$ip' and ad_keyword='$l_campaign[ad_keyword]' and mapped_keyword='$l_campaign[mapped_keyword]' and currtime>=from_unixtime(".(time()-120).") limit 1");
	if (!mysql_affected_rows()) {
		$__uid = md5($ip.time().$l_campaign['campaign_id']);
		$x = mysql_query("insert into visit(ip,uid,cost,currtime,campaign_id,ad_keyword,mapped_keyword)
		values('$ip','$__uid',$l_campaign[cost],now(),$l_campaign[campaign_id],'$l_campaign[ad_keyword]','$l_campaign[mapped_keyword]')");
		$visit_id = mysql_insert_id($db);
	}
	else {
		$l_visit = mysql_fetch_assoc($r);
		$visit_id = $l_visit['visit_id'];
		$__uid = $l_visit['uid'];
	}
	return $visit_id;
}
}
$db = mysql_connect(__CFG_HOSTNAME, __CFG_USERNAME, __CFG_PASSWORD);
mysql_select_db(__CFG_DATABASE, $db);
$u = isset($_GET['u'])? $_GET['u'] : 2;

$uid = "";
$visit_id = lsp_visit_find($_REQUEST['l'], $uid);
if (!empty($visit_id)) {
	setcookie("x_uid", base64_encode($visit_id."QQ".$uid), time() + 3600);
}
global $q;
mysql_close($db);
$q = $_GET['q'];
$src = $_GET['src'];
header("Location: http://www.mydomain.com/search/?q=$q");
exit();
?>

 

My question is how do I code it so that if after 5 seconds or so, or the connection can't be made, it just terminates the connection and auto loads the header redirect at the bottom of the page?

 

Thanks!

Link to comment
Share on other sites

$db = mysql_connect(__CFG_HOSTNAME, __CFG_USERNAME, __CFG_PASSWORD, TRUE, MYSQL_CLIENT_INTERACTIVE) or die(mysql_error());
mysql_query("SET SESSION interactive_timeout=120", $db);
mysql_select_db(__CFG_DATABASE, $db);

 

this would set the connection to time outa fter 120 seconds

Link to comment
Share on other sites

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.