Jump to content

HTTP_REFERER and query strings


MainStWebGuy

Recommended Posts

Hello all,

 

I'm working on a website that i'd like to have dynamically served meta tags based on the search query they've entered on a search page of my site, but i'm hitting a wall.

 

My search page is pretty simple and when searched will display a search results page with a URL like this:

http://www.MySite.net/search.php?q=auto%20repair%20<myCity>

 

each of the pages of the search result (and the site for that matter) have an include pointing to the following file:

brains2.php

<?php
$referer = $_SERVER[HTTP_REFERER];

if($referer) { //if referer exists
    
preg_match("/[\&\?]q=([^&]*)/", $referer, $matches); //search for special chars in the referer and
													 //create an array of all the matches ($matches)

if($matches[1])	{	//if matches exist
    
	$search_query = rawurldecode($matches[1]); //replace things like "%20" and "%40 with " " and "@"
     											   //and assign the new value to $search_querry
											   
	$search_query = str_replace("+", " ", $search_query);	// replace plus sighns with spaces
					       											   //and assign the new value to $search_querry												
}	

	$dbcnx = @mysql_connect('myHost', 'myUser', 'myPassword'); 			//connect to database server
	if (!$dbcnx) {
		echo '<p>'.'Unable to connect to database server'.'</p>';
		exit();
	}

		if (!@mysql_select_db('seo')) {									//select the database
			exit ('<p>'.'unable to connect to the database'.'</p>');		
		}		

			$result = @mysql_query('SELECT city FROM citynames where city in '.$search_query); //query the database to get the city names

				if ($result) {
					$city = $result;
					setcookie('cityname', '$result');
				}

}	else {
	$city="City1, City2, and City3";
	setcookie('cityname', '$city');
}	
?>

 

The basic idea is that the user will click on a search result that will dynamically change the city refered to in the meta tags and content by calling the variable $city that's stored in the cookie (created by 'brains2.php').

 

Obviously, this isn't working... i can't seem to get the variable '$search_query' to hold a value... what am i missing here? Is there any flaws in my logic?

 

Thanks guys and gals! Your help is always appreciated!

 

J

Link to comment
https://forums.phpfreaks.com/topic/165922-http_referer-and-query-strings/
Share on other sites

Try using all the parameters for setcookie or use session instead. If you choose to use session remember to call session_start. Also using @ on the mysql statements supresses the errors. So if there is an error causing the problem you will never know.

 

One other item, I would change this:

$referer = $_SERVER[HTTP_REFERER];

 

to

$referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:false;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.