mysql - Log-in page php redirect and cookie deletion -


i've fixed login page create user entry insert statement , logout button on next page delete user issue when click log-in user becomes registers page not redirect next page automatically, have refresh page leads next page because user seen logged in. code:

<head>  <?php if(isset($_post["logout"])){     $saveuser = $_cookie["user"];     $savepass = $_cookie["password"];     $hostname='localhost';     $username='root';     $password='';      try {         $dbh = new pdo("mysql:host=$hostname;dbname=cs266db_db1",$username,$password);         $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); // <== add line         $sql3 = "delete userid    user='".$saveuser."' , password='".$savepass."'";         if ($dbh->query($sql3)) {             echo "<script type= 'text/javascript'>alert('logged out');</script>";         }         else{             echo "<script type= 'text/javascript'>alert('data not successfull.');</script>";         }          $dbh = null;     }     catch(pdoexception $e) {         echo $e->getmessage();     }      unset($_cookie["user"]);         unset($_cookie["password"]);     } ?>  <?php if(isset($_post["submit"])){     $cookie_user = $_post["user"];     $cookie_pass = $_post["password"];     setcookie("user", $cookie_user, time() + (86400), "/");     setcookie("password", $cookie_pass, time() + (86400), "/");      $hostname='localhost';     $username='root';     $password='';     try {         $dbh = new pdo("mysql:host=$hostname;dbname=cs266db_db1",$username,$password);          $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); // <== add line         $sql2 = "insert userid    values ('".$cookie_user."','".$cookie_pass."')";         if ($dbh->query($sql2)) {             echo "<script type= 'text/javascript'>alert('register complete');</script>";         }         else{             echo "<script type= 'text/javascript'>alert('data not successfull.'); </script>";         }         $dbh = null;     }      catch(pdoexception $e) {         echo $e->getmessage();     } } ?>  <?php if(!isset($_cookie["user"])) {     echo"<form action="."''"."method="."post".">";     echo"<input type='"."text'"." name='"."user'"." placeholder='"."enter username'"." required/><br><br>";     echo"<input type='"."password'"." name='"."password'"."  placeholder='"."enter password'"." required/><br><br>";     echo"<input type='"."submit'"." name='"."submit'"." value='"."  register'"."/>";     echo"</form>";       } else {     header("location: index_1.php"); /* redirect browser */     exit(); } ?>  </head> 

also when log out user deleted correctly when load page netbeans takes me index_1 because seems cookie not deleted? great! again question how redirect correctly index_1 , how delete cookie when log out.

in occurrences of closing , opening php tag, remove blank line. also, remove <head> tag, , blank lines , spaces on top of code (thanks, @rasclatt)

long story short, should have continuous php code. cause of problem described here. not see problem because turned off error reporting.

instead of:

. . . ?>  <?php . . . 

should be:

. . .   . . . 

alternative solution: use javascript redirect instead of http redirect.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -