Jump to content

Recommended Posts

Hello again guys.

 

Someone can tell me whats whrong with this code? it almost all ok but the sessions doesn't work. When i start the session and put a value on it why is it allowing the user to "vote" again?

 

 

It should be calling the echo shouldn't it?

 

<?php

$connection = mysql_connect("localhost","root","");
if($connection){
mysql_select_db("votes");
$pega = mysql_query("SELECT * FROM mariolink");
$row = mysql_fetch_array($pega);


}
else{
echo "erro";}
if(isset($_POST['link'])){
if(!empty($_SESSION['ok'])){
echo "sorry you have already voted";
}
mysql_query("UPDATE `mariolink` SET `link` = `link`+1");
session_start();
$_SESSION['ok'] = 1;


}
if(isset($_POST['mario'])){
if(!empty($_SESSION['ok'])){
echo "Sorry but you have already voted";
}
mysql_query("UPDATE `mariolink` SET `mario` = `mario`+1");
session_start();
$_SESSION['ok'] = 1;


}

?>
<html>
<head>
</head>
<body>
<?php echo "Mario has ";echo $row['mario']; echo" Votes"; ?>
<br>
<?php echo "Link has ";echo $row['link'];echo" Votes"; ?>
<form method="post" action="">

<image src="link.jpeg"/>
<br>
<input type="submit" name="link" value="Choose Link">
<br>
<br>
<image src="mario.jpeg"><br>
<input type="submit" name="mario" value="Choose Mario">
<br>
<br>						
<br><br></form>
</body>


</html>

Edited by neptunerain
Link to comment
https://forums.phpfreaks.com/topic/271501-sessions-arent-working/
Share on other sites

if(isset($_POST['link'])){
if(!empty($_SESSION['ok'])){
echo "sorry you have already voted";
}
mysql_query("UPDATE `mariolink` SET `link` = `link`+1");
session_start();
$_SESSION['ok'] = 1;

You're checking if it's empty and echoing if it isn't. But if it is you aren't preventing it. Use an else { there

 

if(!empty()) {
} else {
}

Edited by SocialCloud

Ok i tried putting the else{} but it still doesn't work :(.

 

The code:

 

if(isset($_POST['link'])){
if(!empty($_SESSION['ok'])){
echo "sorry you have already voted";
}
else{
mysql_query("UPDATE `mariolink` SET `link` = `link` +1");
session_start();
$_SESSION['ok'] = 1;

}
}

Your session_start() statement must come before any reference to a $_SESSION variable.

 

While you can use a session variable to prevent accidental resubmission of data, you cannot use it to prevent intentional resubmission of data because all it takes to get a new chance is to drop the session id cookie and you get a new session.

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.