Suchy Posted April 22, 2008 Share Posted April 22, 2008 I am having trouble with sessions in php 5.2.3, when I press on any of the links insted of jumping to that part of the page I see the login form again. <?php session_start(); ?> <html ... <?php if ( $_POST["user"]=="" && !($action=="enter") ) { ?> <form method="post"> User :<br><input type="text" id="user" name="user"/> <br> Pass:<br><input type="password" id="pass" name="pass"/><br> <input type="submit" name="Enter" value="Enter"/> </form> <?php }else{ $user = $_POST["user"]; $pass = $_POST["pass"]; session_start(); if (($user=="a") && ($pass=="a")) $action="enter"; $_SESSION["ses_action"] = $action; if($action=="enter") { ?> <a href="index.php?op=0">a</a> <a href="index.php?op=1">b</a> <a href="index.php?op=2">b</a> <?php switch($op) { case "0": ?> aaaaaaaaaaaaa <?php break; case "1": ?> bbbbbbbbbbbbb <?php break; case "2": ?> ccccccccccccc <?php break; } } }?> .../html> What seems to be the problem ? Link to comment https://forums.phpfreaks.com/topic/102370-problem-with-sessions/ Share on other sites More sharing options...
947740 Posted April 22, 2008 Share Posted April 22, 2008 You only need to start the session once. You do not need to open it again and again. When you have if($action=="enter"), change it to if($_SESSION['ses_action'] =="enter") Link to comment https://forums.phpfreaks.com/topic/102370-problem-with-sessions/#findComment-524261 Share on other sites More sharing options...
Fadion Posted April 23, 2008 Share Posted April 23, 2008 You only need to start the session once. You do not need to open it again and again. When you have if($action=="enter"), change it to if($_SESSION['ses_action'] =="enter") $action = 'enter'; $_SESSION["ses_action"] = $action; if($action == 'enter'){ //which means if('enter' == 'enter') So u're right about the session, but not for the second part @Suchy, your code look ok to me, except the session_start() thing and that uve no put an id, name and action to the form (the first 2 shouldnt cause problems, but im not sure about the action"). Be sure to have the right input name for "user". I dont see the meaning of using $action; u could only see if $_POST['user'] != '' and $_POST['pass'] != '' and thats all. Oh and !($action=="enter") can be easily written $action != 'enter' Link to comment https://forums.phpfreaks.com/topic/102370-problem-with-sessions/#findComment-524655 Share on other sites More sharing options...
947740 Posted April 23, 2008 Share Posted April 23, 2008 If he goes to that page again, from a page other than the login page, you need to use the session to check if the user is logged in. Otherwise the code will not work. Link to comment https://forums.phpfreaks.com/topic/102370-problem-with-sessions/#findComment-524891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.