Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting information from a url 1

Status
Not open for further replies.

richclever

IS-IT--Management
Oct 5, 2000
127
FR
I have been asked to write a script that takes info from a url and does some processing. Easy enough originally as the url was named index.php?id='idnumber'

However, now the people who want this want to display friendly url's, so if a url was index.php?id=82 it is now pagename.82.0.html I still need to get the 82 from the url and need to know how to strip it. I presume it is something to do with explode but can't figure it out.

Thanks

Rich
 
you use explode() on "." or whatever seperates it.

I would however do like this:
page/friendly/id_82.html

then you make an array of the exploded values, and check keys and currents.

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks Olav,

Thpught it must be something like that. Unfortunately I can't change what the url looks like because the cms system only allows friendly url's in the format I gave.
 
if you did code the page as pagename.82.0.html you would have to have an actual file with that name or are you intending trapping it before it gets to the web server (e.g. ans ISAPI filter under IIS)
 
ingresman: He might use the .htaccess way, as in -> search friendly url.

There is a part I and part II of that script.

Thanks to chessbot on this forum, I made a nice function that makes /foo_1/bar_2.html into: ?foo=1&bar=2

I think that we need to know how your script works.
Does it run queries on db, or do you simply include files?
I run queries, and therefore, I loop thru the keys and then assign $var with current value of the key in the looping array..

sorry, it's hard to explain better, so I post some sample code here:

Code:
// Evolt - Search Engine Friendly URLs Article
// Author : Garrett Coakley : evolt@polytechnic.co.uk

///////// FUNCTIONS /////////////

// processURI(): 
// Takes the query string and extracts the vars by splitting on the '/'
// Returns an array $url_array containg keys argN for each variable.
function processURI() {
global $REQUEST_URI;
global $SCRIPT_NAME;
$path = $SCRIPT_NAME;
$base_href = dirname($path);
$vars = str_replace($path, "", $REQUEST_URI);
$array = explode("/",$vars);
$num = count($array);
for ($i =  1; $i < $num ; $i++) {
$url_array["arg".$i] = $array[$i];
}
//$page = $url_array["arg1"];
	return $url_array;				// return our new shiny array
//return $page;
}

function getURI() {
global $REQUEST_URI;
global $SCRIPT_NAME;
$path = $SCRIPT_NAME;
$base_href = dirname($path);
$vars = str_replace($path, "", $REQUEST_URI);
return $vars;
}



///////// END OF FUNCTIONS /////////////


// Init the page, run processURI and assign it.
$myarray = processURI();
Code:
function getURI() {
global $REQUEST_URI;
global $SCRIPT_NAME;
$path = $SCRIPT_NAME;
$base_href = dirname($path);
$vars = str_replace($path, "", $REQUEST_URI);
return $vars;
}

// ######## get the variable out of the querystring###########
$first = 0;
$last = strlen(getURI());

if (strpos(getURI(), "/") == 0) { 
	// trim first
	$first = 1;
  }
if (strrpos(getURI(), "/") == (strlen(getURI()) - 1)) {
	// trim last
	$last = strlen(getURI()) - 1;
  }
$pageurl = substr(getURI(), $first, $last);
$str = str_replace("/", "&", $pageurl); // Kunstnere_3&foo_2&na_2Array
$str = str_replace("_", "=", $str); // Kunstnere=3&foo=2&na=2Array
parse_str($str, $arr);
//print_r($arr) . "<br />";

while ($pt = key($arr)) {
   if ($pt == 'sub') {
       $sub = str_replace(".html", "", current($arr));
   }
   else if ($pt == 'mnu') {
       $mnu = str_replace(".html", "", current($arr));   
   }
   next($arr);
}
//######### end get variable out of querystring #################



ps. please tip me if there are any optimalizations to be made. I know the loop might be better with foreach, instead of while?

it works like this:
yourdomain.com/foo/bar_1/etc_2.html

then it removes yuordomain.com/foo/

foo is a php file, without extension. You use .htaccess to force it to run as php.

Then, you turn bar_2/etc_2.html into:
bar_3&etc_2
then, you turn that into a querystring like:
bar=3&etc=2

Then, it makes an array of that "querystring" and loops it.
in the loop, it looks for some certain keys..
like, if you need the variable user_id to run a query on a certain page, and you need the variable page_id to choose which page, you do like this:

Code:
while ($pt = key($arr)) {
   if ($pt == 'id') {
       $sub = str_replace(".html", "", current($arr));
   }
   else if ($pt == 'user_id') {
       $mnu = str_replace(".html", "", current($arr));   
   }
   next($arr);
}

you can add more patterns like that..
it replaces .html with nothing, as this is not an html file..

the path is in other words fake!

you will soon notice that all images, etc. are destroyed!!
You need now to add images like this:

Code:
<img src="<? echo "$base_href/images/css.gif"; ?>">

here is the .htaccess file you need:
Code:
<files ~ "^\.ht">
	Order allow,deny
	Deny from all
</files>

<Files site>
	ForceType application/x-httpd-php
</Files>
DirectoryIndex site

If you wish to call the domain/SCRIPTFILE something else than site, you need to change in htaccess.. then simply replace "site" with what you call your script page..

eg. if you want to have:
yourdomain.com/vacation

Code:
<files ~ "^\.ht">
	Order allow,deny
	Deny from all
</files>

<Files [b]vacation[/b]>
	ForceType application/x-httpd-php
</Files>
DirectoryIndex [b]vacation[/b]

remember: then, the file has to be called vacation, without extension!! no .php trailing in the name!!

ps. you will need access to run .htaccess on your hosting.

This should work out of the box..

if you however wish to run includes, based on the variables, I would recommend you looking at the example at as this is modified by yours truly!

Good luck!

ps. I have yet to used this in a stable production, but I have ran several tests on it, and it has worked perfectly.

If you wonder why it's needed:

google will not index pages like:
yourdomain.com?id=2&subcat=4 properly!

Who goes one google and searches for: "subcat=2" ?

noone!!

If you use database, you make those fake urls with your wanted search-terms as close to the domain as possible.

the "site" script, should be called something which identifies your page!

maybe, if you have a car forum, call it car_forum.
Then, when you are to add the variables to the querystring, add first searchterms, like:

/car_forum/pictures/members/volvo/pid_3/userid_3

Then, google will be very optimized for that page! You get a lot more searchterms!

Some people say it does not matter, but actually it does.

Real life example:

this is a search for "volvo power team" on imagesearch.
It does not crawl images on my pages good, since I use urls with id, etc.

search with direct filename:

the image is located at:

However, if I search for: articles concept s60awd:

You see?? There the images are!

I cant wait untill I have rebuilt my page to a more google-friendly system. I will also have to remake my image-parsing scripts, as they are also not good for google.

I hope this might open some eyes, before people paint themselves into a corner.

Olav Alexander Mjelde
Admin & Webmaster
 
addition to my post:
I dont think you can do like:
yourdomain.com/site/page_id_3, as I think then it will be page == NULL and id == 3..

eg. modification might be needed for optimalization of this code.

You can however do:
page-id_3

eg. the _ will be turned into =, and then you harvest out the variables based on which terms you put in the while loop.

You might want to make it a Switch instead of if/elseif, as it's more nice to look at. I however think it's not an important issue, unless you easily get confused by nested if's

also, I saw I posted one function twice, as you probobly know, you only need the function once in your script.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top