guarana1 Posted May 21, 2014 Report Share Posted May 21, 2014 (edited) Estou com algumas dificuldades em passar as minhas url's para friendly url's. Por exemplo, tenho uma galeria de imagens. Cada imagem tem um link destes. item.php?foto=$nomefoto Como é que passos isso para friendly url? E já agora, como é que depois acedo ao valor da variavel? E já agora, friendly URL's oferece alguma proteção contra mysql injection? Edited May 21, 2014 by guarana1 Link to comment Share on other sites More sharing options...
nelsonr Posted May 21, 2014 Report Share Posted May 21, 2014 Será que o que pretendes é URL routing? http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/ Link to comment Share on other sites More sharing options...
guarana1 Posted May 21, 2014 Author Report Share Posted May 21, 2014 Sim, é. Link to comment Share on other sites More sharing options...
bioshock Posted May 21, 2014 Report Share Posted May 21, 2014 Para limpar o URL, por norma utilizo esta função: public static function cleanUrl($str, $replace=array(), $delimiter='-') { setlocale(LC_ALL, 'en_US.UTF8'); if(!empty($replace)) { $str = str_replace((array)$replace, ' ', $str); } $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); $clean = strtolower(trim($clean, '-')); $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); return $clean; } Link to comment Share on other sites More sharing options...
guarana1 Posted May 21, 2014 Author Report Share Posted May 21, 2014 Para limpar o URL, por norma utilizo esta função: public static function cleanUrl($str, $replace=array(), $delimiter='-') { setlocale(LC_ALL, 'en_US.UTF8'); if(!empty($replace)) { $str = str_replace((array)$replace, ' ', $str); } $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); $clean = strtolower(trim($clean, '-')); $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); return $clean; } Podias esclarecer-me como é que se usa essa função sff? Link to comment Share on other sites More sharing options...
bioshock Posted May 21, 2014 Report Share Posted May 21, 2014 Crias um ficheiro em PHP bem como uma classe.. class __global{ public static function cleanUrl($str, $replace=array(), $delimiter='-') { setlocale(LC_ALL, 'en_US.UTF8'); if(!empty($replace)) { $str = str_replace((array)$replace, ' ', $str); } $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); $clean = strtolower(trim($clean, '-')); $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); return $clean; } } Depois é só usar. require_once('global.php'); echo "<a href='" . __global::cleanUrl($variavel_com_texto) . "'>Texto..</a>"; Link to comment Share on other sites More sharing options...
guarana1 Posted May 21, 2014 Author Report Share Posted May 21, 2014 Por exemplo, "<a href='" . __global::cleanUrl($variavel_com_texto) . "'>Texto..</a>"; <a href="http://lisboawines.com/item.php?cod=<?php echo $row['cod'];?> "> Link </a> Como é que ficava? Link to comment Share on other sites More sharing options...
bioshock Posted May 21, 2014 Report Share Posted May 21, 2014 Bem, na verdade não deve ficar assim. O objectivo da função é evitar caracteres especiais no URL. O que estás a passar aí é o código do item, pelo que não deve haver problemas. Link to comment Share on other sites More sharing options...
guarana1 Posted May 21, 2014 Author Report Share Posted May 21, 2014 O que queria era retirar o item.php?variavel=valor Link to comment Share on other sites More sharing options...
bioshock Posted May 21, 2014 Report Share Posted May 21, 2014 (edited) Se utilizasses uma framework com paradigma de programação MVC, como por exemplo o CodeIgniter, isso seria fácil. Se fizeres tudo à lá mão não é assim tão linear. No entanto, o teu href pode ser traduzido para: <a href="http://lisboawines.com/item/<?php echo $row['cod'];?>"> Link </a> Mas vais precisar de fazer um parse do URL para obter o código, ou seja, não vais poder utilizar a função $.GET. Uma função que te pode ajudar: /* Parse URL into pieces Array ( [base] => / [call_utf8] => user/matthew/edit [call] => user/matthew/edit [call_parts] => Array ( [0] => user [1] => matthew [2] => edit ) [query_utf8] => language=en&hobbies=art&sport=football [query] => language=en&hobbies=art&sport=football [query_vars] => Array ( [language] => en [hobbies] => art [sport] => football ) ) */ public static function pathParse() { $path = array(); if (isset($_SERVER['REQUEST_URI'])) { $request_path = explode('?', $_SERVER['REQUEST_URI']); $path['base'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/'); $path['call_utf8'] = substr(urldecode($request_path[0]), strlen($path['base']) + 1); $path['call'] = utf8_decode($path['call_utf8']); if ($path['call'] == basename($_SERVER['PHP_SELF'])) { $path['call'] = ''; } $path['call_parts'] = explode('/', $path['call']); @$path['query_utf8'] = urldecode($request_path[1]); @$path['query'] = utf8_decode(urldecode($request_path[1])); $vars = explode('&', $path['query']); foreach ($vars as $var) { $t = explode('=', $var); @$path['query_vars'][$t[0]] = $t[1]; } } return $path; } Edit: E claro, para que isto funcione, tens de criar condições no .htaccess. Edited May 22, 2014 by bioshock Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now