This question has already been answered:

I can not figure out the connection with which constantly on the main page 404 PAGE is written, instead of HELLO GUEST or just INDEX (if the user is authorized) In the file library \ main.php itself:

function getUrlSegment($num){ $url = strtolower($_GET['url']); $urlSegments = explode('/',$url); return $urlSegments[$num]; } 

On the home page:

 <?php ini_set('error_reporting', E_ALL); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); session_start(); require_once 'core/library/db.php'; require_once 'core/configs/main.php'; require_once 'core/library/main.php'; require_once 'core/library/validator.php'; require_once 'core/models/category.php'; require_once 'core/models/post.php'; //$url = (isset($_GET['$url']) ? $_GET['$url'] : 0); $cntrName = (is_null(getUrlSegment(0))) ? 'main' : getUrlSegment(0); $actionName = (is_null(getUrlSegment(1))) ? 'action_index' : 'action_'.getUrlSegment(1); if(file_exists('core/controllers/'.$cntrName.'.php')){ require_once 'core/controllers/'.$cntrName.'.php'; if(function_exists($actionName)){ $actionName(); }else{ show404page(); } }else { show404page(); } 

In controler:

  <?php function action_index(){ if(is_auth()){ echo 'INDEX PAGE'; }else{ echo 'Hello guest!'; } } function action_contact(){ echo 'CONTACT PAGE'; } function action_registration(){ if($_SERVER['REQUEST_METHOD'] == 'POST'){ $formData = [ 'login' => getSaveData(htmlspecialchars(trim($_POST['login']))), 'password' => getSaveData(trim($_POST['password'])), 'email' => getSaveData(trim($_POST['email'])) ]; $rules = [ 'login' => ['required', 'login'], 'password' => ['required','password'], 'email' => ['required','email'] ]; $errors = validateForm($rules, $formData); if(empty($errors)){ $formData['password'] = md5($formData['password'].SECRET_KEY); //$sql = "INSERT INTO `user` (`login`, `password`, `email`) VALUES ('{$formData['login']}','{$formData['password']}','{$formData['email']}') ON DUPLICATE KEY UPDATE login='{$formData['login']}', password='{$formData['password']}', email='{$formData['email']}'"; $sql = "INSERT INTO `user`(`login`, `password`, `email`) VALUES ('{$formData['login']}','{$formData['password']}','{$formData['email']}')"; $sql1 = "SELECT id FROM user WHERE login='{$formData['login']}' or email='{$formData['email']}'"; $res = selectData($sql1); if($res->num_rows === 0){ } if(insertUpdateDelete($sql)) { header("Location: /main/successReg"); }else{ echo 'Пользователь с таким логином или почтой уже существует. :('; } } } renderView('registration'); } function action_successReg(){ echo 'Поздравляем!'; } function action_login() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { $formData = [ 'login' => getSaveData(htmlspecialchars(trim($_POST['login']))), 'password' => getSaveData(trim($_POST['password'])), ]; $formData['password'] = md5($formData['password'] . SECRET_KEY); $sql = "SELECT id FROM user WHERE login='{$formData['login']}' and password='{$formData['password']}'"; $res = selectData($sql); if ($res->num_rows === 0) { echo 'Некоректный логин или пароль!'; } else { $_SESSION['user'] = mysqli_fetch_assoc($res); header('Location: /'); } } renderView('login', []); } function action_logout(){ session_unset(); session_destroy(); header('Location: /'); } 

Reported as a duplicate by participants Alexey Shimansky , Visman , Cheg , Duck Learns to Hide , br3t July 31, '17 at 11:33 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    in particular, the problem is most likely due to gaps in front of <?php - Alexey Shimansky
  • Make sure your code works correctly. No warning output. - Visman

1 answer 1

need to fix notice:

 function getUrlSegment($num){ $url = (isset($_GET['url'])) ? strtolower($_GET['url']) : null; $urlSegments = explode('/',$url); return (isset($urlSegments[$num])) ? $urlSegments[$num] : null; } 
  • If you fix the code as you wrote it gives an error: "Parse error: syntax error, unexpected ':', expecting ';' in E: \ OpenServer \ OpenServer \ domains \ myblog3.ru \ core \ library \ main.php on line 24 " - user239511
  • @ user239511 ugh, corrected. - tcpack4
  • Errors disappeared. Great, thank you! Only now only 404 page is constantly displayed and if you go to any page where there was a login form, for example, then only "Hello guest!" Is written. What could it be? - user239511
  • @ user239511 in show404page comes in. Check file_exists and function_exists. - tcpack4
  • I checked ... There are only a couple of lines: function action_index () {if (is_auth ()) {echo 'INDEX PAGE'; } else {echo 'Hello guest!'; }} The whole project uploaded to disk. If you have time and desire, please help me figure it out. yadi.sk/d/T26Ho66f3LZ2JL - user239511