B
B
baracuda22014-11-24 23:33:26
PHP
baracuda2, 2014-11-24 23:33:26

Which cnc version is better?

I was looking for how to make cnc, in the end I wrote it myself. A couple of hours later I came across a ready-made version from an amateur-professional.
Actually the question is which option is better to choose?
on speed
on loading
on protection

I think that my variant though on newer, but will lose.

<?php
class Router {
    private $_route = array(); 
    public function setRoute($dir, $file) {
        $this->_route[trim($dir, '/')] = $file;
    }
    public function route() {
        if (!isset($_SERVER['PATH_INFO'])) { 
            include_once 'index.html'; 
        } elseif (isset($this->_route[trim($_SERVER['PATH_INFO'], '/')])) { 
            include_once $this->_route[trim($_SERVER['PATH_INFO'], '/')]; 
        }
        else return false; 
        return true;
    }
}
$route = new Router;
$route->setRoute('page/name', "1.html"); 
$route->setRoute('page-2', "2.html");
$route->setRoute('post', "3.html");
if (!$route->route()){echo '404';}

htaccess
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1

The script is not completed, I decided to simplify and therefore without a base.
If it fits better, then I will continue to add it, just connect the base)

And the script itself found
<?php
  $mysqli = new mysqli("localhost", "root", "", "lesson-local");
  function replaceSEF($content) {
    global $mysqli;
    $regex = "[(<a[^>]*href\s*=\s*[\"'])([^'\"]*)([\"'][^>]*>\s*.*?\s*</a>)]i";
    preg_match_all($regex, $content, $matches);
    for ($i = 0; $i < count($matches[2]); $i++) {
      $temp = str_replace("&amp;", "&", $matches[2][$i]);
      $result_set = $mysqli->query("SELECT `alias` FROM `sef` WHERE `link`='$temp'");
      $row = $result_set->fetch_assoc();
      $content = str_replace($matches[2][$i], $row["alias"], $content);
    }
    if ($result_set) $result_set->close();
    return $content;
  }
  function getContent($alias) {
    global $mysqli;
    $content = "Моя страница <a href='/?view=article&amp;id=1'>тут</a> и ещё вот <a href='/?view=section&amp;id=1'>это</a>";
    $result_set = $mysqli->query("SELECT `link` FROM `sef` WHERE `alias`='$alias'");
    $row = $result_set->fetch_assoc();
    if ($result_set) $result_set->close();
    $vars = parse_url($row["link"]);
    parse_str($vars["query"], $vars);
    if ($vars["view"] == "article") $content .= "<br />Статья с ID=".$vars["id"];
    elseif ($vars["view"] == "section") $content .= "<br />Раздел с ID=".$vars["id"];
    else $content = "404 Not Found";
    return $content;
  }
  
  
  $content = getContent(substr($_SERVER["REQUEST_URI"], 1));
  echo replaceSEF($content);
  $mysqli->close();
?>

htaccess
AddDefaultCharset UTF-8

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /?%{QUERY_STRING}

It is already ready and works with the base

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bromzh, 2014-11-25
@bromzh

Escobar.zhpg. Both are terrible.
See how it is implemented in normal frameworks.

X
xmoonlight, 2014-11-25
@xmoonlight

1. Lots of unnecessary activities in the classroom. Why put paths in a class if they will only be used once?! switch is not easier to put?
2. No need to trim 100 times in a row on the same variable - all necessary transformations before branching.
3. Save immediately to 2 global variables: service name and service variables. (explode by the name of the service, and take it as the 2nd parameter: [1])
4. Get rid of the class - it's superfluous here. (two global variables are enough)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question