adrianmak Posted January 19, 2015 Share Posted January 19, 2015 This is my code for frontend user login <?php $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; if ($user->isLoggedin()) { $session->redirect($config->urls->root . $lang); } if ($input->post->user && $input->post->pass) { $user = $sanitizer->username($input->post->user); $pass = $input->post->pass; if ($session->login($user, $pass)) { $session->redirect($config->urls->root . $lang); //$template = $page->template; //echo "Login successful. You are in $template->name template"; } } $form_title = __("Sign In"); $form_user = __("User"); $form_pass = __("Password"); $form_submit_btn = __("Sign in"); $form_error = __("Login failed"); //if($input->post->user) echo "<h2 class='error'>$form_error</h2>"; $page->title = "User Login"; $page->body .= " <div class='container'> <div class='col-md-12'> <div class='login-box'> <div class='panel panel-default'> <div class='panel-heading'> <h3 class='panel-title'><span class='lock-icon'></span><strong>$form_title</strong></h3> </div> <div class='panel-body'> <form role='form' action='./' method='post'> <div class='form-group'> <label for='user'>$form_user</label> <input type='text' name='user' id='user' class='form-control' placeholder=$form_user /> </div> <div class='form-group'> <label for='pass'>$form_pass</label> <input type='password' name='pass' id='pass' class='form-control' placeholder=$form_pass /> </div> <button type='submit' class='btn btn-sm btn-default'>$form_submit_btn</button> </form> </div> </div> </div> </div> </div> "; include('./main.php'); ?> The login page show properly. However, when login with a wrong username or password, it returned internal server error Anything wrong with my code? Link to comment Share on other sites More sharing options...
adrianmak Posted January 20, 2015 Author Share Posted January 20, 2015 while read the pw logs, it said that Error: Call to a member function isLoggedin() on a non-object (line 42 of /var/www/html/pw1/site/templates/main.php) <?php $signin_text = __("Sign in"); $logoff_text = __("Logout"); ?> <!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <?php echo $pageRefresh; ?> <title><?php echo $page->title; ?></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>assets/css/normalize.css" /> <script src="<?php echo $config->urls->templates?>assets/js/vendor/modernizr-2.6.2.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css"> <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>assets/css/main.css"> </head> <body> <!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <!-- Add your site or application content here --> <nav class="navbar navbar-default navbar-static-top"> <nav class="nav navbar-left"> <div><?php echo $page->title; ?> | Learing processwire</div> </nav> <?php $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; if ($user->isLoggedin()) { echo <<< _OUT <nav class="nav navbar-right"> Welcome $user->name<br/> <a href="{$config->urls->root}{$lang}user/logoff">$logoff_text</a> </nav> _OUT; } else { echo <<< _OUT <div class="navbar-right"> <a href="{$config->urls->root}{$lang}user/login">$signin_text</a> </div> _OUT; } ?> </nav> <div class="header"> <ul class="nav nav-pills"> <?php $homepage = $pages->get('/'); foreach($homepage->and($homepage->children) as $item) { if($item->id == $page->rootParent->id) echo "<li class='current'>"; else echo "<li>"; echo "<a href='$item->url'>$item->title</a></li>"; } ?> </ul> <ul class="languages"> <?php $currentLanguage = $user->language; // remember language foreach($languages as $language) { if(!$page->viewable($language)) continue; // is page viewable in this language? $user->language = $language; if($language->id == $currentLanguage->id) { echo "<li class='current'>"; } else { echo "<li>"; } echo "<a href='$page->url'>$language->title</a></li>"; } $user->language = $currentLanguage; // restore language ?> </ul> </div> <div class="content"> <?php echo $page->body; ?> </div> <?php $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; // echo $lang; ?> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <script>window.jQuery || document.write('<script src="<?php echo $config->urls->templates?>assets/js/vendor/jquery-1.10.2.min.js"><\/script>')</script> <script src="<?php echo $config->urls->templates?>assets/js/plugins.js"></script> <script src="<?php echo $config->urls->templates?>assets/js/main.js"></script> <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. --> <script> (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date; e=o.createElement(i);r=o.getElementsByTagName(i)[0]; e.src='//www.google-analytics.com/analytics.js'; r.parentNode.insertBefore(e,r)}(window,document,'script','ga')); ga('create','UA-XXXXX-X');ga('send','pageview'); </script> </body> </html> What's wrong with that line in my main.php ? Link to comment Share on other sites More sharing options...
Jan Romero Posted January 20, 2015 Share Posted January 20, 2015 Ah. You’re overriding ProcessWire’s $user variable here: $user = $sanitizer->username($input->post->user); Now $user is just some string the visitor entered, and no longer the special user object. Just use a different variable like $username or something. The same thing applies for other API variables like $page, $pages, $session, etc. as well, of course. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now