Jump to content

[SOLVED] $_POST['hello'] Versus $_SESSION['world'] Conflict problem


suttercain

Recommended Posts

Good afternoon everyone,

 

I am running into an issue with my $_SESSION and $_POST. I will attempt to explain my set up as best I can.

 

First Page:

The user enters a 4 digit id number:

<form action="submit_review.php" method="post">
"<input name='comic_id' type='text' value='' size='5' maxlength='4'></td>";
Type in the four digit comic ID number.
<input type='submit' name='submit'>
</form>

 

Based on that four digit ID number the user is taken to page two where they are presented with a 2 field form. The title that corisonds to the id Number they entered, along with the comic_id is echoed so the user can verify the comic they were looking for is accurate.

 

Second Page:

<?php
session_start();
include ('../header.html');
$comic_id = $_POST['comic_id'];

include ('get_connected.php');
    $sql = "SELECT * FROM comics WHERE comic_id = '" . mysql_real_escape_string($comic_id) . "'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
      }
    }
if ($row ==!NULL) {
echo "You entered Comic ID #$comic_id. This number refers to " . $row['title'] . " #" . $row['issue_number'] . " ";
echo "<br>";
} else {
echo "<font color='red'><b>Sorry a comic matching ID number #$comic_id could not be located in the database. Please check the number and try again.</b></font>";
}

?>
<br><br><?php
if ($vbulletin->userinfo['usergroupid'] == '6') {
echo "<form action='preview_reviews.php' method='post'>";
}
?>
REVIEW:<br>
<textarea name="review" cols="80" rows="20"><?php echo stripslashes($_SESSION['review']); ?></textarea><br><br>
RATING:<br>
<input type="text" name="rating" value="<?php echo $_SESSION['rating'] ?>"></input><br>
<input type="hidden" name="comic_id" value="<?php echo $_SESSION['comic_id'] ?>"></input><br>
<input type="submit" name="submit" value="submit"/>
<?php
include ('../footing.html');
$_SESSION['title'] = $row['title'];
?>

 

Once they have filled out both forms on page two they hit submit and are taken to page three, a preview page. This allows the user to check for mistakes in spelling, etc. If the information is accurate they can hit submit again and the data is then processed into the database. If the data is in correct they can hit the "Go Back and Edit" link.

Page Three:

<?php
session_start();
include ('../header.html');
$_SESSION['review'] = $_POST['review'];
$_SESSION['rating'] = $_POST['rating'];
$_SESSION['comic_id'] = $_POST['comic_id'];
$comic_id = $_SESSION['comic_id'];
$review = $_SESSION['review'];
$rating = $_SESSION['rating'];
$title = $_SESSION['title'];

echo "$title<br><br>
  $review<br><br>
  $comic_id<br><br>
  $rating";
  
?>
<form action="final_reviews.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
<a href="submit_review.php">Go Back to Edit</a>
<?php include ('../footing.html'); ?>

 

 

This is the problem. If the user decides to go back instead of submitting the information, from page 3, the review field and rating field are stored in sessions and echoed back into the form fields.. which is good. But the comic_id number is lost along the way. Pretty sure it's a conflict of the $_POST['comic_id'] and the $_SESSION['comic_id']. I have spent two evening trying to rectify this without have to bother all of you good people, but I am stuck and must...

 

Can someone point me to the solution, so that if I chose to "Go Back and Edit" the comic_id is retained... without it, no one can edit their information.

 

Any advice or suggestions? Thank you in advance.

 

SC

Link to comment
Share on other sites

I think the problem is that you use $_POST['comic_id'] on page 2.  This is fine when page 2 is accessed after submissions of page 1, but not when it is accessed from page 3's "Go back".  So you need to check if $_POST['comic_id'] or $_SESSION['comic_id'] has the data you want, or you can check how you were called.  You could do:

 

if (!empty($_POST['comic_id'])) {
  $comic_id = $_POST['comic_id'];
} elseif (!empty($_SESSION['comic_id'])) {
  $comic_id = $_SESSION['comic_id'];
} else {
  # No comic_id from any source
}

 

That will take post data in priority over session data.  I'm also suspicious of your use of $_SESSION at the bottom of page 2.  It looks like the session data is only set in page 3, so that data would be blank if going directly from page 1 (did you clear your session data regularly while testing?)

Link to comment
Share on other sites

Hi Btherl,

 

Thanks for responding. I ran this code:

 

if (!empty($_POST['comic_id'])) {
  $comic_id = $_POST['comic_id'];
  echo "1";
} elseif (!empty($_SESSION['comic_id'])) {
  $comic_id = $_SESSION['comic_id'];
  echo "2";
} else {
echo "3";
}

 

When I go from page 1 to page 2, it echos 1.

When I go from page 3 back to page 2. I get a 1.

 

So it's not set.

 

Do you know how I would solve this issue? Would I have to re-do most of the code?

 

Thanks again for your time.

 

SC

Link to comment
Share on other sites

I also just ran this on both pages 2 and 3:

 

<?php
if (isset($_SESSION['comic_id'])) {
echo "Session seems set.";
} else {
echo "Session Not Set.";
}
?>

 

From Page " " to " "

1 to 2 echoed Session Seems Set

2 to 3 echoed Session Seems Set

3 to 2 echoed Session Not Set

 

I have no idea how to get this thing done... bizzaro world.

Link to comment
Share on other sites

I just ran this:

 

if (isset($_SESSION['comic_id'])) {
echo "Session seems set.";
} else {
echo "Session Not Set.";
}
if (isset($_POST['comic_id'])) {
echo "Session seems set.";
} else {
echo "Session Not Set.";
}
if (isset($comic_id)) {
echo "Session seems set.";
} else {
echo "Session Not Set.";
}

 

Same result on all three, same as the above post. When it gets back to page 2 it all gets unset... anyone know why?

 

Thanks.

Link to comment
Share on other sites

That's interesting..

 

The session variable should NOT be set when going from page 1 to page 2.  It gets set by page 3.  So that is probably a left-over session from earlier.  You can make a script that calls session_destroy() to clear out the data before you do each lot of testing.

 

My guess is that the session variable is unset by the code in page 3, because that's the only code which could do it.  Can you try checking its value both before and after the code that sets $_SESSION['comic_id'], to see if it's there that it gets changed?

 

Regarding the $_POST['comic_id'], that will only be set if you submit comic_id in a form.  It's expected that it will not be set if you follow a standard "a href" link.

Link to comment
Share on other sites

Hi Btherl,

 

This is how I solved it,

 

if(isset($_POST['comic_id']))
{
   $comic_id = $_POST['comic_id'];
   $_SESSION['comic_id'] = $comic_id;
}
elseif(isset($_SESSION['comic_id']))
{
   $comic_id = $_SESSION['comic_id'];
}
else
{
   $comic_id = '';  
   echo "some type of error";
} 

 

Seems to be working as desired. Thank you for your time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.