Jump to content

Problem with sessions


Suchy

Recommended Posts

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

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' :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.