sayedsohail Posted July 29, 2007 Share Posted July 29, 2007 i am just trying to figure out why my session is rejecting my first attempt, than on my second attempt it shows registered. config.php <? # Admin Panel User Name $uname = "admin"; # Admin Panel Password $pword = "test"; ?> index.php <? require ("config.php"); session_start(); if ((!$username) || (!$password)) { echo '<form name=login method=post action=""> user:<input type=text name=username><br> pass:<input type=text name=password><br> <input type=submit value=go> </form>'; } else { if ($username=="$uname") { session_register("username"); session_register("password"); echo "user is $uname, and password is $pword <br> <a href=\"?m=1\" >unreg</a>"; } else echo "nope"; } if ($m==1) { session_unregister("username"); session_unregister("password"); } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted July 29, 2007 Share Posted July 29, 2007 put session_start() at the top and the requre undeneth try. Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted July 29, 2007 Author Share Posted July 29, 2007 done that, still the same. Quote Link to comment Share on other sites More sharing options...
lightningstrike Posted July 29, 2007 Share Posted July 29, 2007 session register is deprecated use the new standard of session superglobal. e.g. $_SESSION["username"] = "value"; and rather then session_unregister use unset e.g. unset($_SESSION["username"]); etc. also when comparing two variables in if ($username=="$uname") { you should not use quotes if ($username==$uname) { <?php require ("config.php"); session_start(); if ((!$_POST["username"]) || (!$_POST["password"])) { echo '<form name=login method=post action=""> user:<input type=text name=username><br> pass:<input type=password name=password><br> <input type=submit value=go> </form>'; }else{ if ($_POST["username"]==$uname && $_POST["password"]==$pword) { $_SESSION["username"] = $_POST["username"]; $_SESSION["password"] = $_POST["password"]; echo "user is $uname, and password is $pword <br> <a href=\"?m=1\" >unreg</a>"; } else echo "nope"; } if ($_GET["m"]==1) { unset($_SESSION["username"]); unset($_SESSION["password"]); } Do NOT use register_globals its dangerous for server security. Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted July 29, 2007 Author Share Posted July 29, 2007 The reason i register the username using session_register("USERNAME"), is to validate in the following pages, now i change to $_SESSION["username"] = $_POST["username"]; once registered i am redirecting to members.php file, unfortunately any link <a href..> i click in members.php, it brings back to index.php and i have to enter the login details again, its just happening on my first attempt. But on my second attempt after input of login details, it redirects as usual to the members.php and all <a href...> links works fine. Although i am using session_start(); at the top of all my php files right after the <?php. I am not sure why my first attempt it rejects. i.e, all the pages got this lines <?php session_start(); if(isset($_SESSION['username']) == TRUE) do something else redirect to login/index.php page. .. ?> Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted July 29, 2007 Author Share Posted July 29, 2007 Any suggestions:? Quote Link to comment Share on other sites More sharing options...
lightningstrike Posted July 29, 2007 Share Posted July 29, 2007 the script i provided you works fine on my setup, session_register has no benefits... Quote Link to comment Share on other sites More sharing options...
btherl Posted July 30, 2007 Share Posted July 30, 2007 Sayed, I tested your script here and it works on the first try. The only oddity is I have to click "unreg" twice to unregister the session. This is because the username and password are unregistered AFTER being tested. Just to confirm, do you remove the "m=1" before testing the script each time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.