I want to add this code but show error

Malik Umer Farooq Posted in Component Development 7 years ago

i want add this code but show error i add this code in malik_secret component remove all other code then add

> A system error has occurred. Please try again later. You may email the details of this error to the system administrator at [email protected].

    if(!ossn_isLoggedin(){
    require_once "class/detect.php";
$detect = new Mobile_Detect();
if ($detect->isMobile()) {
    rediect("login")
    exit();
}

please help me sir
here is detect.php fille
https://github.com/Lablnet/PHP-Mobile-Detection/blob/master/detect.php

Replies
pk Malik Umer Farooq Replied 7 years ago

Ok sir thanks for help me like a tutorials

German Michael Zülsdorff Replied 7 years ago

Ok,
1. I downloaded your component and installed it successfully.
2. i enabled the component and my site crashed with error 500

So, the next step a developer would do is look into the error logfile, and yes there's a helpful line:

PHP Parse error:  syntax error, unexpected '{' in /var/www/ossn/components/Malik_Secret/ossn_com.php on line 6

So, the next step a developer would do is correcting that line: in this case adding the missing bracket.
And refresh the the page. Which is still giving an error 500.

So, the next step a developer would do is look into the error logfile, and yes there's a helpful line:

Parse error:  syntax error, unexpected 'exit' (T_EXIT) in /var/www/ossn/components/Malik_Secret/ossn_com.php on line 11

So, the next step a developer would do is correcting that mistake: in this case adding the missing semicolon on line 10.
And refresh the the page. Which is still giving an error 500.

So, the next step a developer would do is look into the error logfile, and yes there's a helpful line:

PHP Parse error:  syntax error, unexpected end of file in /var/www/ossn/components/Malik_Secret/ossn_com.php on line 17

So, next a developer would remember that formatting the source correctly is of great help to avoid unmatched brackets and use his editor highlighting to find what's missing ...
Having done that, the code would look like

function Malik_Secret_init() {
    ossn_register_page('Secret', 'Malik_Secret_pages');

    if(!ossn_isLoggedin()){
        require_once "class/detect.php";
        $detect = new Mobile_Detect();
        if ($detect->isMobile()) {
            rediect("login");
            exit();
        }
    }
}

And no surprise: my admin panel is back to life again. Now, that I'm still logged in, let's see what happens when I log off, as your component deals with not logged in users. And Ooops:
I'm getting a blank page!

So, the next step a developer would do is look into the error logfile, and yes there's a helpful line:

PHP Fatal error:  require_once(): Failed opening required 'class/detect.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/ossn/components/Malik_Secret/ossn_com.php on line 7

So, the next step a developer would do is asking: Why can't that file be opened? And the answer is simple: Because it's not there. In the class directory is in fact no file named detect.php, there's a file named Malik_Secret.php.

So, the next step a developer would do is either change the file name in the source accordingly, or rename the file in the class directory as expected. So let's decide for the latter one and change Malik_Secret.php to detect.php.

Success! No more blank page. But still no redirection ... because maybe the browser is too wide
... So let's try either a real cellphone or use the chrome browser's developer console to emulate a cell phone. And refresh. And Oops: We're getting a white page again!

So, the next step a developer would do is look into the error logfile, and yes there's a helpful line:

PHP Fatal error:  Call to undefined function rediect() in /var/www/ossn/components/Malik_Secret/ossn_com.php on line 10

And in fact Ossn has no function named rediect(), the correct name is redirect(). So let's correct that typo! And refresh the page. But oops - we're getting a "Too many redirects" error now.

So, the next step a developer would do is look into his code and search for logical errors. So what's going wrong? The redirect seems to work now, but since the user is still logged off, he'll be redirected to the login page again and again. So we need to add a check that the user will be only redirected from the index page. All in all the final code would look like this then:

<?php

function Malik_detect_init() {
    if(!ossn_isLoggedin()){
        require_once "class/detect.php";
        $detect = new Mobile_Detect();
        if ($detect->isMobile() && $_SERVER['REQUEST_URI'] == '/') {
            redirect("login");
            exit();
        }
    }
}

ossn_register_callback('ossn', 'init', 'Malik_detect_init');

> Please note: This was a ONE Time tutorial. You see that EVERY error
> will be protocolled exactly in the error log. And all you have to do
> is reading that log and correct what you did wrong. So there's no need
> to ask again and again for help, as this can't be the place for
> teaching programming.

German Michael Zülsdorff Replied 7 years ago

If you want help provide at least a repository with your complete component. Nobody is able to understand what it means "remove all other code" ... A component needs at least one function to get initialized ... and not just fragments.