php - Login Script Difficulties? -
i'm hoping can me login script. code works when run wamp localhost when goes live on server returns blank page when try , login can tell me why? code follows.
<?php session_start(); //login script if(isset($_request['ch']) && $_request['ch'] == 'login'){ //give login credentials here if($_request['uname'] == 'zipbuzz' && $_request['pass'] == 'sewilt') $_session['login_user'] = 1; else $_session['login_msg'] = 1; } //get page name redirect if(isset($_request['pagename'])) $pagename = $_request['pagename']; //logout script if(isset($_request['ch']) && $_request['ch'] == 'logout'){ unset($_session['login_user']); header('location:login.php'); } if(isset($_session['login_user'])){ if(isset($_request['pagename'])) header('location:'.$pagename.'.php'); else header('location:admin.php'); }else{ ?> <?php //display error msg if login credentials wrong! if(isset($_session['login_msg'])){ echo 'wrong username , password !'; unset($_session['login_msg']); } ?>
then add following pages need login access.
<?php session_start(); //check logged in or not! if(!isset($_session['login_user'])){ header('location:login.php?pagename='.basename($_server['php_self'], ".php")); } ?>
first up, here's corrected version of code posted.
<?php session_start(); //login script if(isset($_request['ch']) && $_request['ch'] == 'login') { //give login credentials here if($_request['uname'] == 'zipbuzz' && $_request['pass'] == 'sewilt') $_session['login_user'] = 1; else $_session['login_msg'] = 1; } //get page name redirect if(isset($_request['pagename'])) $pagename = $_request['pagename']; //logout script if(isset($_request['ch']) && $_request['ch'] == 'logout') { unset($_session['login_user']); header('location:login.php'); } if(isset($_session['login_user'])) { if(isset($_request['pagename'])) header('location:'.$pagename.'.php'); else header('location:admin.php'); } else { //display error msg if login credentials wrong! if(isset($_session['login_msg'])) { echo 'wrong username , password !'; unset($_session['login_msg']); } } ?>
besides that, there several lines use session_write_close();
make sure session data and/or status indeed saved/updated before redirect url. didn't implement in above correction though. take training opportunity. ;)
Comments
Post a Comment