session - PHP remember url parameter value using $_SESSION -
i made script shows value of "school_id" in url parameter.
http://mywebsite.com/mygrade?school_id=00000 i use $_get['school_id'] display id number.
<?php echo $_get['school_id']; ?> but want if parameter "school_id" empty, want display previous data entered.
example, user browse http://mywebsite.com/mygrade?school_id=00000 browse http://mywebsite.com/mygrade?school_id= id has no value. still display 00000 previous id used.
i used code below doesn't work.. :(
<?php session_start(); $_session['schoo_id'] = $_get['school_id']; if ($_get['school_id'] === null || $_get['school_id'] == ""){ echo $_session['schoo_id']; } else{ $_get['school_id']; } ?> anyone point , me?
i'm going break down line line, please let me know in comments if need explain further:
self explanatory:
<?php session_start(); there typo here:
$_session['schoo_id'] = $_get['school_id']; but! fixing won't resolve problem. happens if $_get['school_id'] not defined/blank? guess what, $_session['school_id'] blank. don't want behavior, you'll want only set $_session['school_id'] if $_get['school_id'] defined
accessing $_get['school_id'] throw e_notice error if isn't defined, you'll want instead check existence, rather checking see if null.
if ($_get['school_id'] === null || $_get['school_id'] == ""){ oh, typo intended. why misspell school though? no need! :)
echo $_session['schoo_id']; what doing? nothing! no echo, nothing. accessing variable , doing nothing it.
} else{ $_get['school_id']; } ?> here's code should like, or @ least believe intend:
<?php session_start(); if (isset($_get['school_id']) && $_get['school_id'] !== ""){ $_session['school_id'] = $_get['school_id']; } // $_session['school_id'] guaranteed $_get['school_id'] (if set) // or whatever last time defined // echo it. echo $_session['school_id']; ?>
Comments
Post a Comment