Page 1 of 1

URL class

Posted: Sun Jun 05, 2005 1:55 pm
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 ;)

Posted: Sun Jun 05, 2005 2:14 pm
by Smackie
um this isnt a place to rate codes :? lol

Posted: Sun Jun 05, 2005 2:17 pm
by Roja
Looks like many people miss EvilWalrus.. :(

Posted: Sun Jun 05, 2005 2:18 pm
by anjanesh
1. You should've put it in the Code Snippets Section.
2. You could've added poll with rating options.

Posted: Sun Jun 05, 2005 2:20 pm
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.

Posted: Sun Jun 05, 2005 4:22 pm
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...

Posted: Mon Jun 06, 2005 5:07 am
by JAM
Moved...

Posted: Mon Jun 06, 2005 2:01 pm
by xfish5
Hello, I need help with url class.
Have you any proposition, tips or note to modification?
Security,...?

Thanx