thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Part 49 - Using PHP to Handle a Clean URL ~ 2 #165

Open shicala opened 8 years ago

shicala commented 8 years ago

Continuing from https://github.com/thedigicraft/Atom.CMS/issues/163

No 404 issue anymore but....

I dont get the 'call' it talks about

Array ( [base] => /AtomCMS [call_utf8] => home [call] => [call_parts] => Array ( [0] => )

[query_utf8] => [query] => [query_vars] => Array ( [] => )

like the video shows I should, and typing the different slugs doesnt load a different page.

creptor commented 8 years ago

Probably the get_path function has an error... or is not complete. This is my code, so some things are change, but it's the same thing.

check with this code:

function get_path(){
    $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']);
        if(array_key_exists(1,$request_path)){
            $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);
                if(array_key_exists(1,$t)){
                    $path['query_vars'][$t[0]]=$t[1];
                }
            }
        }
    }
return $path;
}
shicala commented 8 years ago

no change really

Array ( [base] => /AtomCMS [call_utf8] => home [call] => [call_parts] => Array ( [0] => )

)

and thats using http://shicala.com/AtomCMS/home as the path if I change it to http://shicala.com/AtomCMS/faq for example i still get the home page but get this as the path array

Array ( [base] => /AtomCMS [call_utf8] => faq [call] => [call_parts] => Array ( [0] => )

)

creptor commented 8 years ago

Give me a minute, I'm trying to figure out your problem.

creptor commented 8 years ago

Can you please add the error_reporting(-1); to the top of your setup.php, I'm not able to find the issue here.

I'm looking at your page by the way.

shicala commented 8 years ago

added, not seeing anything spit out

I'm almost ready to skip the clean urls >.<

creptor commented 8 years ago

:( Could you please add a var_dump($request_path);inside of the get_path function? please

creptor commented 8 years ago

I think I found something.... It's about the IIS (I now hate IIS :c ). You need to change the $_SERVER['REQUEST_URI'] to $_SERVER['PATH_INFO'], (as google says :3 ). link

I don't know if this will fix all the issues but it's something.

shicala commented 8 years ago

did both posts above (sry for delay went to take a nap)

first one makes it say 'NULL' at the top of my screen

second doesnt seem to change much

creptor commented 8 years ago

Ok, this is the last thing I have found :c . So you should undo all the changes you made from this $_SERVER['REQUEST_URI'] to $_SERVER['PATH_INFO'] (basically redo the get_path function with this ), and then add this code before the code calls to the get_path function. If you place it on the top of your setup.php it would be even better.

Code:

if (!isset($_SERVER['REQUEST_URI']))
{
       $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
       if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
}
shicala commented 8 years ago

didn't change much... if anything

http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

this site here talks about a module, do i need such a thing? would my host have to install such a thing? or is it something I would have to?

creptor commented 8 years ago

The thing is that it isn't the url anymore... let me explain..

So in the issue #163 you actually defined a configuration for the server to accept "clean urls", and it works :D , but now you're stuck in the code. As I read in a page I closed and don't want to search for it again, there're some thing that IIS changes from the default Apache servers in the PHP server side language. So some predetermined variables are not equivalent in Apache as in IIS, and that's where the error is (and why it outputs nulls values).

Since I have just heard of IIS 20 hours ago, I don't know a thing about it (I kind of hate it now :c ) and so it's very hard to me to figure out the problem, or error in this case, BUT I'm extremely sure it has to do with the get_path function, more specifically, with the $_SERVER predefined variable.

So yea, IIS is really hard. Windows is really hard in some ways.

But, I got a new idea writing this..... you could do a var_dump($_SERVER) and place it here :D , that way I know what type of constants the variable has, and what're they for, so I could rewrite the get_path to work with IIS.

I have to say though, I'll be just guessing when creating that code.

shicala commented 8 years ago

Error

The website encountered an unexpected error. Please try again later. Error messagePDOException: SQLSTATE[28000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'stickney_ad7user'. in db_table_exists() (line 2764 of E:\HostingSpaces\stickney\shicala.com\wwwroot\includes\database\database.inc).

thats what I get instead of any of the page, I'll note however that the db it's refering to doesnt exist.

creptor commented 8 years ago

I don't know what you just did... but that's drupal.. I think.

shicala commented 8 years ago

you asked me to put

var_dump($_SERVER)

I put it in right after the

$path=array(); like this

$path=array();
var_dump($_SERVER);
var_dump($request_path);
creptor commented 8 years ago

maybe try to place it above everything. so it doesn't get stop by the error, and still outputs the result.

shicala commented 8 years ago

I put it above everything right after the <?php

same error, sadly I gonna have to continue this battle tomorrow though I have to go to work now

ty for your time and efforts I will check back to this in the morning

creptor commented 8 years ago

Seems that the variable is blocked from the server side... I'll come back tomorrow with some ideas.

creptor commented 8 years ago

maybe this will do it... try it please

function get_path(){
    $path=array();
    $_SERVER['REQUEST_URI'] = $_SERVER['ORIG_PATH_INFO'];
    if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING']; }
    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']);
        if(array_key_exists(1,$request_path)){
            $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);
                if(array_key_exists(1,$t)){
                    $path['query_vars'][$t[0]]=$t[1];
                }
            }
        }
    }
return $path;
}
shicala commented 8 years ago

the last one gave me some more input into the path array now i get

Array

( [base] => /AtomCMS [call_utf8] => index.php/home [call] => index.php/home [call_parts] => Array ( [0] => index.php [1] => home )

[query_utf8] => 
[query] => 

)

but if I change the URL to like http://shicala.com/AtomCMS/faq

I still see the main home page and get this in the path array info

Array

( [base] => /AtomCMS [call_utf8] => index.php/faq [call] => index.php/faq [call_parts] => Array ( [0] => index.php [1] => faq )

[query_utf8] => 
[query] => 

)

creptor commented 8 years ago

I can't seem to help you further with this... (tried to recreate the error with a local hosted server, and failed....bugs) But, I believe that's enough to make the CMS work.

you see, when calling for a page, instead of using the $path["call_parts"][0] you would need to use this $path["call_parts"][1], which is the value next to the always present index.php.

Also If I'm not wrong, the redirect code will also work.... so you shouldn't have any problems with that.

Redirect code:

$mainPage="home;//-> you should fetch this from the database, and having this in a variable can be useful
$serverHttp=$_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"]."/";
if($path["call_parts"][0]==""||$path["call_utf8"]=="index.php"){
    header("Location: ".$serverHttp.$mainPage);
}

For more help I recomend checking out http://stackoverflow.com/ , there you'll probably find someone more qualify than me in IIS for working PHP on it. They could give you a better answer for this problem.

shicala commented 8 years ago

where does that redirect code go?

creptor commented 8 years ago

The redirect goes to the main page you set (in this case http://somethingfunny.com/home)... I'm sorry but that was my approach in my code.. so instead of having a default id or something, I just redirect the user to it... This avoided a lot of errors for me, but you can redo it as you like.

I don't remember much of the way @thedigicraft approach this, so sometimes I give unexpected results, sorry.

Just remember everything should work, as long as you change the $path["call_parts"][0] to $path["call_parts"][1] when looking for the page.

Example: In the setup.php paragraph 32, in the $page... It should be $page = data_post($dbc, $path['call_parts'][1]);

The redirect is an adjusted version of :

if(!isset($path['call_parts'][0]) || $path['call_parts'][0] == '' ) {
    //$path['call_parts'][0] = 'home'; // Set $path[call_parts][0] to equal the value given in the URL 
    header('Location: home');  
} 

So all the variables are customizable easily (and gets the url set correctly).

shicala commented 8 years ago

lol I meant where in the code XD

creptor commented 8 years ago

in the setup.php :3

Example: In the setup.php paragraph 32, in the $page ... It should be $page = data_post($dbc, $path['call_parts'][1]); The redirect is an adjusted version of : ....