php - Android sharedpreference logout don't clear values -
when i'm testing login on mobile app editor.clear();
not working guess because whenever logout , click login without values on username , password still logs in previous 1 can guys me fix problem? here codes.
loginfragment
public class loginfragment extends fragment implements compoundbutton.oncheckedchangelistener { final string tag = this.getclass().getname(); button btnlogin; edittext etusername, etpassword; textview tvregister; checkbox cbremember; sharedpreferences pref; sharedpreferences.editor editor; boolean checkflag; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_login, container, false); etusername = (edittext) v.findviewbyid(r.id.etfirstname); etpassword = (edittext) v.findviewbyid(r.id.etpassword); btnlogin = (button) v.findviewbyid(r.id.btnlogin); tvregister = (textview) v.findviewbyid(r.id.tvregister); cbremember = (checkbox) v.findviewbyid(r.id.cbremember); cbremember.setoncheckedchangelistener(this); checkflag = cbremember.ischecked(); etusername.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { log.d(tag, etusername.gettext().tostring()); } }); pref = getactivity().getsharedpreferences("login.conf", context.mode_private); editor = pref.edit(); final string username = pref.getstring("username", ""); final string password = pref.getstring("password", ""); hashmap postdata = new hashmap(); postdata.put("username", username); postdata.put("password", password); if (!username.equals("") && (!password.equals(""))) { postresponseasynctask task1 = new postresponseasynctask(getactivity(), postdata, new asyncresponse() { @override public void processfinish(string s) { if (s.contains("renter")) { log.d(tag, s); toast.maketext(getactivity(), "renter login successful!", toast.length_short).show(); intent in = new intent(getactivity(), rentertabs.class); startactivity(in); getactivity().finish(); } else if (s.contains("owner")) { log.d(tag, s); toast.maketext(getactivity(), "owner login successful!", toast.length_short).show(); intent in = new intent(getactivity(), ownertabs.class); startactivity(in); getactivity().finish(); } else { toast.maketext(getactivity(), "wrong username or password...", toast.length_short).show(); } } }); task1.execute("http://carkila.esy.es/carkila/authenticate.php"); } btnlogin.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { hashmap postdata = new hashmap(); postdata.put("username", etusername.gettext().tostring()); postdata.put("password", md5.encrypt(etpassword.gettext().tostring())); postresponseasynctask task1 = new postresponseasynctask(getactivity(), postdata, new asyncresponse() { @override public void processfinish(string s) { log.d(tag,s); if (s.contains("renter")) { if (checkflag) { editor.putstring("username", etusername.gettext().tostring()); editor.putstring("password", md5.encrypt(etpassword.gettext().tostring())); editor.apply(); log.d(tag, pref.getstring("password", "")); } intent in = new intent(getactivity(), rentertabs.class); startactivity(in); getactivity().finish(); } else if (s.contains("owner")) { if (checkflag) { editor.putstring("username", etusername.gettext().tostring()); editor.putstring("password", md5.encrypt(etpassword.gettext().tostring())); editor.apply(); } editor.putstring("username", etusername.gettext().tostring()); editor.putstring("password", md5.encrypt(etpassword.gettext().tostring())); editor.apply(); intent in = new intent(getactivity(), ownertabs.class); startactivity(in); getactivity().finish(); } else { toast.maketext(getactivity(), "wrong username or password...", toast.length_short).show(); } } }); task1.execute("http://carkila.esy.es/carkila/authenticate.php"); } }); tvregister.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent in = new intent(getactivity(), registeractivity.class); startactivity(in); } }); return v; } @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { checkflag = ischecked; log.d(tag, "checkflag: " + checkflag); } }
class logout
public class ownertabs extends appcompatactivity { private sectionspageradapter msectionspageradapter; private viewpager mviewpager; sharedpreferences pref; sharedpreferences.editor editor; floatingactionbutton fab; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_owner_tabs); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); pref = getsharedpreferences("login.conf", context.mode_private); mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); mviewpager.setoffscreenpagelimit(2); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent in = new intent(ownertabs.this, insertactivity.class); startactivity(in); } }); mviewpager.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { } @override public void onpageselected(int position) { switch (position){ case 0: fab.show(); break; default: fab.hide(); break; } } @override public void onpagescrollstatechanged(int state) { } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main,menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if(id == r.id.action_logout){ editor = pref.edit(); editor.clear(); editor.commit(); intent in = new intent (getapplicationcontext(), mainactivity.class); startactivity(in); finish(); } return super.onoptionsitemselected(item); }
authenticate.php
<?php require 'database-config.php'; session_destroy(); $username = ""; $password = ""; if(isset($_post['username'])){ $username = $_post['username']; } if (isset($_post['password'])) { $password = $_post['password']; } $q = 'select * tbl_user username=:username , password=:password'; $query = $dbh->prepare($q); $query->execute(array(':username' => $username, ':password' => $password)); if($query->rowcount() == 0){ header('location: login.php?err=1'); }else{ $row = $query->fetch(pdo::fetch_assoc); $_post['sess_user_id'] = $row['userid']; $_post['sess_userrole'] = $row['roles']; if($_post['sess_userrole'] == "renter"){ echo "renter"; }else if ($_post['sess_userrole'] == "owner"){ echo "owner"; } } ?>
database.config.php
<?php $database = '****'; $host = '****'; $user = '****'; $pass = '****'; $dbh = new pdo("mysql:dbname={$database};host={$host}", $user, $pass); try{ if(!$dbh){ echo "unable connect database"; } }catch (pdoexception $e){ echo $e->getmessage(); } ?>
you need call editor.commit();
after editor.clear();
method
Comments
Post a Comment