URL class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xfish5
Forum Newbie
Posts: 13
Joined: Fri Jun 03, 2005 1:32 pm

URL class

Post by xfish5 »

Hello, here is my class for work with URL. Rate it, please!

Code: Select all

<?php 
class CUrl {
	var $anchor; 
        var $param = array();
        var $script_name;
        var $protocol;
        var $separator;
        var $add_sid;
  
function CUrl() {
	$this->script_name = $_SERVER[SCRIPT_NAME];
	$this->param = $_GET;
	$this->separator = '&';
	$this->add_sid = true;
	
	if($this->add_sid) $this->setParam(session_name(), session_id());
}
 
function setParam($name='',$val='') {
		$this->param[$name] = $val;
}

function setAnchor($anchor='') {
		$this->anchor = $anchor;
}
  
function dropParam($name='') {
		unset($this->param[$name]);
}
  
function setScriptName($script_name) {
	$this->script_name = $script_name;
}

function getUrl() {
	return $_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
}  
  
function makeUrl() {
$res = $this->script_name;
	if (count($this->param)>0) 
		$res .= '?';
   	$tarr = array();
   foreach ($this->param as $key=>$val) {
   	$tarr[] = $key.'='.(urlencode($val));
	} 
      $res .= implode($this->separator,$tarr);
   return $res;
   }
    
function makeLink($name='',$title='', $class='') {
	echo '<a href="'.$this->makeUrl().'#'.$this->anchor.'"';
	if($title) echo ' title="'.$title.'"';
	if($class) echo ' class="'.$class.'"';	
	echo '>'.$name.'</a> ';
}

}// end class 
?>
Thanks ;)
Smackie
Forum Contributor
Posts: 302
Joined: Sat Jan 29, 2005 2:33 pm

Post by Smackie »

um this isnt a place to rate codes :? lol
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Looks like many people miss EvilWalrus.. :(
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

1. You should've put it in the Code Snippets Section.
2. You could've added poll with rating options.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Pros:

1. Good naming conventions (except for the name of the class, see #2 in cons)

2. Very simple

Cons:

1. Okay, first, you should indent the whole thing properly: don't suddenly drop indents or add them without reason. All the variables should be indented similarly. Indentation is your friend. ;)

2. Next, don't call it CUrl. That is extremely close to CURL, which is a totally different thing, and can confuse people.

3. Document your class via PHPDoc syntax. Even if you never actually generate documentation from it, you should document all your stuff, makes it easier for other people and yourself.

4. Next, you're not validating your parameter names. This means that if someone sends a cleverly designed URL to someone else and they click a link, it could arbitrarily execute something (an XSS attack). You should know exactly what parameters you're passing on, rather than passing them all on by default.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

- The code forgot to use the $protocol...

- You might want to improve your getURL too :) (http://timvw.madoka.be/programming/php/geturl.txt is what i'm using...)


- I don't think that makeLink belongs in the class...
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Moved...
xfish5
Forum Newbie
Posts: 13
Joined: Fri Jun 03, 2005 1:32 pm

Post by xfish5 »

Hello, I need help with url class.
Have you any proposition, tips or note to modification?
Security,...?

Thanx
Post Reply