(?p=home) includes pages not working - php

I have setup a new server and copyed my website on to it and the follow dont work (?p=home) includes pages.
Code:
if(isset($HTTP_GET_VARS['p']))
{
$page = $HTTP_GET_VARS['p'];
}
else
{
$page = 'home';
}
switch($page)
{
case 'home':
require('home.php');
break;
case 'login':
require('login.php');
break;
default:
echo('Error: There is no file on this server with that name');
}
On the other webhost it was on it worked fine am think in that it is a php.ini config file need editing can anyone help me?

Use the $_GET array instead of $HTTP_GET_VARS. The latter one is deprecated and probably disabled due to register_long_arrays.

This must do the trick :)
if(isset($_GET['p']))
{
$page = $_GET['p'];
}
else
{
$page = 'home';
}

Related

How to get everything after an '#'-selector in the url in php

I'm coding a CDN in PHP, it works this far, but I want to choose different versions by adding an '#' with the version behind it. How do I get the version String after the '#'.
Example: https://cdn.steven2105.de/jquery#3.3.0
<?php
$error = false;
$path = str_replace("/", "", $_SERVER['REQUEST_URI']);
switch($path) {
case "jquery":
$toOpen = "jquery/3.3.1/jquery.min.js";
break;
case "jquery#3.3.0":
$toOpen = "jquery/3.3.0/jquery.min.js";
break;
case "vue":
$toOpen = "vue/2.6.10/vue.min.js";
break;
case "bootstrap-css":
$toOpen = "bootstrap/4.3.1/bootstrap.min.css";
break;
case "bootstrap-js":
$toOpen = "bootstrap/4.3.1/bootstrap.min.js";
break;
case "baguettebox-css":
$toOpen = "baguettebox/1.11.0/baguettebox.min.css";
break;
case "baguettebox-js":
$toOpen = "baguettebox/1.11.0/baguettebox.min.js";
break;
case "popper":
$toOpen = "popper/1.14.7/popper.min.js";
break;
default:
$error = true;
break;
}
if (!$error) {
header("Location: https://cdn.steven2105.de/libs/$toOpen");
} else {
include_once "website.php";
}
EDIT: What should I do now? It doesn't work.
I have different libaries in the path https://cdn.steven2105.de/libs/ e.g. https://cdn.steven2105.de/libs/jquery with files in it such like https://cdn.steven2105.de/libs/jquery/3.3.1/jquery.min.js. I'd like to access these links with a shorter prefix like https://cdn.steven2105.de/jquery#3.3.1. If I use the '#' character it should redirect to the version in the directory https://cdn.steven2105.de/libs/jquery/ and if it's without the '#' it should redirect to the latest version.
What is the easiest way?
You can use explode function for this. It Outputs as an array and fetch it.
<?php
$URL = ' https://cdn.steven2105.de/jquery#3.3.0';
$Result = explode('#', $URL);
echo("<pre>");print_r($Result[1]);
?>
Instead of using PHP you could probably fix this with webserver configuration.
Rewrite rules in Apache (https://httpd.apache.org/docs/2.4/rewrite/remapping.html) should do the trick. I would expect Nginx to offer similar functionality.
If you use Apache or Nginx to do this for you it will be way faster as you won't have the overhead of running a PHP script.

PHP Include - Error Fixing

I have the following PHP code that produces an error as the include files don't exist. I have not yet made them but I want to stop errors being produced (not just hidden). Is there anything I can put into my code that says "don't record any errors if the file doesn't exist, just ignore the instruction"
<?php
$PAGE = '';
if(isset($_GET['page'])) {
$PAGE = $_GET['page'];
};
switch ($PAGE) {
case 'topic': include 'topic.php';
break;
case 'login': include 'login.php';
break;
default: include 'forum.php';
break;
};
?>
use file_exists() to check if your file exists or not before calling include;
if (file_exists('forum.php')) {
//echo "The file forum.php exists";
include 'forum.php';
}
//else
//{
// echo "The file forum.php does not exists";
//}
Include the files only if they exist. You can add a check for existing file -
switch ($PAGE) {
case 'topic':
if(file_exists(path_to_file)) {
include 'topic.php';
}
break;
......
};
file_exists()
You seem to be looking for # operator which silences any errors from an expression, you can read more about it here: http://php.net/manual/en/language.operators.errorcontrol.php
Use file_exists() function:
<?php
$PAGE = '';
if(isset($_GET['page'])) {
$PAGE = $_GET['page'];
};
switch ($PAGE) {
case 'topic':
if (file_exists("topic.php")){
include 'topic.php';
}
break;
case 'login':
if (file_exists("login.php")){
include 'login.php';
}
break;
default:
if (file_exists("forum.php")){
include 'forum.php';
}
break;
};
?>
Documentation http://php.net/manual/en/function.file-exists.php

PHP - Do an else to many ifs?

EDIT: Very simple, figure it out. Just put an else{} without a } before and that will work for both ifs.
Well I wrote this code and it works ok
if($_REQUEST['pag']){
// Variável da página
$pag = $_REQUEST['pag'];
// Possíveis páginas
if($pag == "registrar"){
include "inc/register.php";
}
if ($pag == "recuperar"){
include "inc/recover.php";
}
}
But what if the user types ?pag=ofksaofkoasdkfkopsd ?
The page won't exist, so I want to include a file if that happens.
Is there a way to make this without using a list or something?
you can do this by switch and that would be better way then if because
The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for. (manual )
switch($_REQUEST['pag']){
case "registrar":
include "inc/register.php";
break;
case :"recuperar":
include "inc/recover.php";
break;
default:
//default you want to include
break;
}
i hope you know about the complexity of the code and in your case switch case has less complexity in worst/average case
Try with if with else like
$pag = $_REQUEST['pag'];
// Possíveis páginas
if($pag == "registrar"){
include "inc/register.php";
}
elseif ($pag == "recuperar"){
include "inc/recover.php";
}
else{
include "inc/default.php"
}
Or you can use switch case where you can found default option like
switch($pag)
{
case "register":include "inc/register.php";
break;
case "recuperar":include "inc/recover.php";
break;
default : include "inc/default.php";
}
use if {} else{} or switch case.
You can use nested if else though.
At least give an eye on the docs - http://php.net/manual/en/control-structures.elseif.php
Why don't you want to use an array? That really would be the best way to go IMHO. Adding pages is easier, you get less duplicated code... It would also be trivial to load the array from a config file or DB instead of having it hard-coded in a later stadium. I see only benefits.
$pages = array( "registrar" => "inc/register.php",
"recuperar" => "inc/recover.php" );
$requestedPage = $_REQUEST["pag"];
if(array_key_exists($requestedPage, $pages)){
include $pages[$requestedPage];
}
else {
include "inc/error404.php";
}
some Use the switch command.
switch ($_REQUEST['pag']) {
case 'registrar':
echo "some text or action";
break;
case 'xxx':
echo "some text or action";
break;
case 'xxxx':
echo "some text";
break;
default:
echo "some text";
}
$pag = isset($_REQUEST['pag']) ? $_REQUEST['pag'] : null;
switch($pag) {
case 'registrar':
require 'inc/register.php';
break;
case 'recuperar':
require "inc/recover.php";
break;
default:
// Include your default page
}
In a non-dynamic language like C++ you'll have to use
if() elseif() else to compare non-constant values.
if ($condition1)
{
//code
}
elseif ($condition2)
{
//code
}
elseif ($condition3)
{
//code
}
...
else
{
//this is what happens when no other condition is true
}
switch is a better practice. check #user1752647's answer
Try with the elseif and else
if($pag == "registrar"){
include "inc/register.php";
}elseif($pag == "recuperar"){
include "inc/recover.php";
}else{
... //include a file if the page doesn't exist
}
Or you can do it with a switch
switch($pag){
case "registrar":
include "inc/register.php";
break;
case "recuperar":
include "inc/recover.php";
break;
default:
... //include a file if the page doesn't exist
}
Hope it will help you.
You also can use this code
$pages = array("register", "recover");
if (in_array($_REQUEST["pag"], $pages)) {
include $_REQUEST["pag"].".php";
}
else{
include "default.php";
}

how to include a page in php

I usually use this code below to include the page that I need into the body of my website to include the page when clicking on a link.
<?php
switch($_GET['page']){
case '1':
if(file_exists('main.php'))
{
include_once('main.php');
break;
}
default:
include_once('main.php');
break;
}
?>
but then I have to change this everytime i add a menu item by adding a case '2' ... etc
and now my question can this be written shorter/dynamically so that i just can add a link without having to change the piece of code everywhere?
ps: i did made it a little bit shorter.. but its still not good enough i think..
i also want to add this: i get my links from a ini file. i place it in there like this:
[navigation]
main.php = "Home"
if (!isset($_GET['page'])) {
$_GET['page'] = 'main.php';
}
switch ($_GET['page']){
case 'main.php':
case 'about.php':
case 'portfolio.php':
case 'tips.php':
$file = $_GET['page'];
break;
default:
$file = '404.html';
}
include_once $file;
is it possible to get this too from the ini file?
Try this:
$page = isset($_GET['page']) ? $_GET['page'] : "main.php";
if( file_exists($page)) include($page);
else include("404.html");

php session strange behavior

I have a PHP website with a custom page routing system.
A php script checks the requested uri and uses a switch case to determine which page needs to be loaded. Each page has its own $page_id.
Here's a simplified version.
$page_uri = //code that extracts the relevent part of the uri
switch($page_uri){
case 'about':
$page_id = 'about';
break
case 'products':
$page_id = 'products';
break;
default:
$page_id = '404';
break;
}
include 'sessions.php'; //explanation about this after the code
include $page_id; //code that loads the page based on the $page_id
For one of the site's function, I need to know the $page_id of the previously visited page.
I am using sessions for this.
session_start();
$previous_page_id = $_SESSION['current_page_id'];
$_SESSION['current_page_id'] = $page_id;
I am therefore storing the previous $page_id which is stored in a session variable in the $previous_page_id variable before the session variable gets updated to the current $page_id.
The problem is that it doesn't work. The $previous_page_id is always equal to the default $page_id which is 404. I know the actual routing function works because the right page gets loaded and if I echo the value of the session right after storing it it's correct.
I've noticed that I can get the correct $previous_page_id if I put the following part before everything else.
session_start();
$previous_page_id = $_SESSION['current_page_id'];
What am I missing? Can you think of anything wrong with my code or logic? Is there some weird quirks I should be aware of when it comes to PHP sessions?
Thanks.
Update
It seems adding session_start(); echo $_SESSION['current_page_id']; at the top of the page makes the value stick. Otherwise by the time I get to transfering the session value to the $previous_page_id in sessions.php, the session value has changed to 404.
Can anyone make sense of that?
The problem was coming from a missing favicon.ico file which was triggering a 404 page load after each page load.
You need to put the session_start(); at the beginning of the script, that way the session is started (and available) when you need it in your big switch statement.
You have forgot to add .php extension to your include (+ some smaller things). And remember that session_start must be called at beggining, before any use of it. So you need to call it on beggining or include sessions at beggining.
So it should look like:
session_start();
$page_uri = //code that extracts the relevent part of the uri
switch($page_uri){
case 'about':
$page_id = 'about';
break;
case 'products':
$page_id = 'products';
break;
default:
$page_id = '404';
break;
}
$previous_page_id = $_SESSION['current_page_id'];
$_SESSION['current_page_id'] = $page_id;
include $page_id . '.php'; //code that loads the page based on the $page_id
session_start();
$previous_page_id = $_SESSION['current_page_id'];
$_SESSION['current_page_id'] = $page_id;
I don't really get this. $previous_page_id and $page_id are equal.
You are setting them to be equal.
You should set it after the switch statement as follows:
$page_uri = //code that extracts the relevent part of the uri
if (file_exists('sessions.php')) {
include 'sessions.php';
} else {
trigger_error("'sessions.php' not found", E_USER_ERROR);
}
switch($page_uri) {
case 'about':
$page_id = 'about';
break
case 'products':
$page_id = 'products';
break;
default:
$page_id = '404';
break;
}
if (isset($_SESSION['current_page_id'])) {
$_SESSION['current_page_id'] = $page_id;
} else {
trigger_error("'current_page_id' key not set", E_USER_ERROR);
}
if (isset($page_id)) {
include $page_id;
} else {
trigger_error("'page_id' not set", E_USER_ERROR);
}
with sessions.php being:
$ss = session_start();
if (!$ss) { trigger_error("Session couldn't be started", E_USER_ERROR);
if (isset($_SESSION['current_page_id'])) {
$previous_page_id = $_SESSION['current_page_id'];
} else {
trigger_error("'current_page_id' key not set", E_USER_ERROR);
}

Categories