php - Session variables resets on the refresh of the page -


i`m trying create mock login website whenever try save username in session, when page reloads, resets session 1. put on session.auto_start on thinking session_start issue it's still happening

my header.php

    <?php         if($_session['user'] != 1){             $user = $_session['user'];     ?>         <input type='submit' id='accedit' value='edit account'/>         <label class='clicklog'>welcome: <?=$user?></label>     <?php         }else{     ?>     <input type='hidden' id='accedit' value='edit account'/>     <p id="clicklog">login?</p>     <?php         }      ?> 

the function

 case "login":          $user = $_post['user'];         $pass = $_post['pass'];          if($user = "cody" && $pass = "1234"){             $_session['user'] = $user;             echo "yes";         }else{             echo "no";         }         exit; 

the ajax call

function login(user,pass){     var action = "login";     $.ajax({         url:'appedit.php',         type: "post",         data: {action:action,user:user,pass:pass},         success: function(response){             if(response == "yes"){                 alert(response);                 $("#clicklog").html("<label class='clicklog'>welcome: " + $user + "</label>");                 $("#clicklog").show();                 $("#accedit").prop("type", "submit");             }else{                 alert(response + "fail");                 $("#clicklog").show();             }         },                       error: function (request, status, error) {             alert(error);         }     }); } 

i know it's not best looking code i'm trying patch together.

you're assigning here

if($user = "cody" && $pass = "1234"){          ^                 ^ 

where should comparing

if($user == "cody" && $pass == "1234"){          ^^                 ^^ 

using 2x equal signs.

  • 1 equal sign => assign
  • 2 equal signs => compare
  • 3 equal signs => if identical

consult manual http://php.net/manual/en/language.operators.comparison.php

plus, make sure session indeed started session_start(); inside pages using sessions.

  • use isset().
  • use !empty().

add error reporting top of file(s) find errors.

<?php  error_reporting(e_all); ini_set('display_errors', 1);  // rest of code 

sidenote: error reporting should done in staging, , never production.

  • check console.

additionally if you're planning move forward creating login system don't limit passwords , use proper methods hash , verify passwords php.

if plan continue using ajax function provided jquery should aware return functions have been deprecated , removed soon:

deprecation notice: jqxhr.success(), jqxhr.error(), , jqxhr.complete() callbacks deprecated of jquery 1.8. prepare code eventual removal, use jqxhr.done(), jqxhr.fail(), , jqxhr.always() instead.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -