get search page is not being displayed because of REQUEST_URI

Asked

Viewed 45 times

1

Well I’ll try to get past what happens!

I have a php site where you have the config.php file with the following lines

<?php
$current_page_uri = $_SERVER['REQUEST_URI'];
$part_url = explode("/", $current_page_uri);
$page_name = end($part_url);
$email_id = "[email protected]";
?>

On the index.php page the following configuration

<?php 
include "app/config.php";
include "app/detect.php";

if ($page_name=='') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='index.php') {
    include $browser_t.'/index.php';
}
elseif ($page_name=='sobre.php') {
    include $browser_t.'/sobre.php';
}
elseif ($page_name=='galeria.php') {
    include $browser_t.'/galeria.php';
}
elseif ($page_name=='servicos.php') {
    include $browser_t.'/servicos.php';
}
elseif ($page_name=='contato.php') {
    include $browser_t.'/contato.php';
}
elseif ($page_name=='contato-post.php') {
    include $browser_t.'/contato.php';
    include 'app/contato.php';
}

else
{
    include $browser_t.'/404.html';
}

?>

When I enter the regular pages as about.php, galeria.php the browser normally opens.

Already on the part of http://localhost/meusite/galeria.php I have a problem, there is presented all the photos of products coming from Mysql, and has a pagination to advance the products on the pages.

When I select to advance a product page on which the link takes the page reference that is, example http://localhost/meusite/galeria.php?pagina=2 the site does not open.

If I insert in index.php the line below it works.

elseif ($page_name=='galeria.php?pagina=2') {
    include $browser_t.'/galeria.php';
}

Only that I have a problem also in the search button of the site that can search the product, when I search for example code product : "6554"

when I click search I would have to open the page http://localhost/meusite/galeria_busca.php?id=6554&busca=

only it doesn’t open either, but as a test I inserted the following line in index.php and it worked.

elseif ($page_name=='galeria_busca.php?id=6554&busca=') {
    include $browser_t.'/galeria_busca.php';
}

I would like the help of someone to tell me what I am doing wrong that is not opening normally without putting the search paths in index.php because I have many products and it would be impossible to create a line of code for each product code.

Any doubt I’m available.

  • You take the final part of the url http://localhost/meusite/galeria.php?pagina=2 and assigns the variable $page_name that takes on the value galeria.php?pagina=2 and then compares it to galeria.php , of course it won’t work, that is, there won’t be the include $browser_t.'/galeria.php'; That’s what you’re doing wrong!!

1 answer

1


You take the final part of the url http://localhost/meusite/galeria.php?pagina=2 and assigns the variable $page_name that takes on the value galeria.php?pagina=2 and then compares it to galeria.php , of course it won’t work, that is, there won’t be the include $browser_t.'/galeria.php'; That’s what you’re doing wrong!!

In the $page_name = end($part_url); see if there’s the ?, if there is a explode and take the first part and assign the variable $page_name

A more direct way is to make use of strstr - finds the first occurrence of a string

}elseif (strstr($page_name,"galeria.php")){

ideone example



About your comment ..... na minha .config não tem o "?" .....

When you access http://localhost/meusite/galeria.php?pagina=2 there’s the ? and its .config will return to you galeria.php?pagina=2 and therefore will not enter the elseif ($page_name=='galeria.php') { because $page_name (assumed the value galeria.php?pagina=2) is different from galeria.php



I would rule out the .config and I would do it this way:

<?php 
$current_page_uri = $_SERVER['REQUEST_URI'];
$email_id = "[email protected]";
include "app/detect.php";

if (strstr($current_page_uri,"index.php'")){
    include $browser_t.'/index.php';
}
elseif (strstr($current_page_uri,"sobre.php")){
    include $browser_t.'/sobre.php';
}
elseif (strstr($current_page_uri,"galeria.php")){
    include $browser_t.'/galeria.php';
}
elseif (strstr($current_page_uri,"servicos.php")){
    include $browser_t.'/servicos.php';
}
elseif (strstr($current_page_uri,"contato.php")){
    include $browser_t.'/contato.php';
}
elseif (strstr($current_page_uri,"contato-post.php")){
    include $browser_t.'/contato.php';
    include 'app/contato.php';
}
else
{
include $browser_t.'/404.html';
}
?>
  • Good evening, but I could not understand what I meant, in my . config does not have the "?" is like this. $current_page_uri = $_SERVER['REQUEST_URI']; $part_url = explode("/", $current_page_uri); $page_name = end($part_url);

  • 1

    @Alexsousa, when you access http://localhost/meusite/galeria.php?pagina=2 there’s the ? and its .config will return to you galeria.php?pagina=2

  • Well coming back here now by unlocking the code I could understand what was wrong, and to work in my config I created another line of code by blowing up the "?" and using "strstrstr" as @Leocaracciolo told me, then in my index.php I inserted an elseif with the condition. Thanks Leo for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.