8 Replies - 952 Views - Last Post: 27 November 2011 - 05:13 PM Rate Topic: -----

#1 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

if statement not working

Posted 24 November 2011 - 10:06 AM

Hello im trying to make a scrip were if the user views session vairable more than 10 times they get sent to a cpature image ( stopping bots etc )


<?php

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
?>



<?php



if ( $_SESSION['views'] > 10 ) {

header( 'Location: http://myurl.net/cap/example_form.php' ) ;
}

?>




the views is being set i checked though the print_r function just the last if statementis not working i use the if > all over my website which seems to work..

Is This A Good Question/Topic? 0
  • +

Replies To: if statement not working

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,582
  • Joined: 08-August 08

Re: if statement not working

Posted 24 November 2011 - 10:15 AM

If you don't start a session the $_SESSION array will be not be set.
Was This Post Helpful? 0
  • +
  • -

#3 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: if statement not working

Posted 26 November 2011 - 03:17 AM

i start the session. This if statement is in side a config.php file with a session start and a db connect
<?php
	session_start();
	
mysql_connect("localhost", "", "");
mysql_select_db("");



?>


<?php

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
?>



<?php



if ( $_SESSION['views'] > 1 ) {

header( 'Location: http://mysite.net/cap/example_form.php' ) ;
}

?>




Please read the post be for answering next time please I did say in the first post saying i print the session out and the session is being set.
" the views is being set i checked though the print_r function "

This post has been edited by rpgmaker: 26 November 2011 - 03:17 AM

Was This Post Helpful? -2
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • View blog
  • Posts: 10,582
  • Joined: 08-August 08

Re: if statement not working

Posted 26 November 2011 - 05:28 AM

You need to show your code as it is run. The code you've shown in your first post does not start a session. We can't assume you've done something you claim to have done when the code you show clearly does not do that.

This post has been edited by CTphpnwb: 26 November 2011 - 05:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 hadi_php  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 382
  • Joined: 23-August 08

Re: if statement not working

Posted 26 November 2011 - 10:14 AM

So simple.

NOTE: don't run the code because i am just trying to tell you what u need to do. You will get the idea from here and you have to do it with your own

Let me tell you with example -

index.php

session_start();

function stop_spam($count=10){
$_SESSION['view'] = $_SESSION['view'] + 1;
if($_SESSION['view']>$count) header("Location: captcha.php");
}

if(!$_SESSION['human']) stop_spam();
site code here



captcha.php

if(captcha return TRUE) {
$_SESSION['human'] = TRUE;
header("Location: index.php");
}
else INCORRECT captcha



But i don't prefer to stop spam bot like this. First you can try with htaccess. Then try to detect spambot in a smart way like a HUMAN never sent 10 request in 2 sec. I hope now you get some idea! and for captcha use reCaptcha. :)

This post has been edited by hadi_php: 26 November 2011 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: if statement not working

Posted 27 November 2011 - 04:58 PM

Dude, your first if statement dosen't have { } those brackets. Plus the logic is flawed.

Have you set the session view to a value before this script is to be runned?

if no, then the isset will return false and not run that code so your session view won't be set that is why the second if statement won't work.

Also, from what I see your trying to count how many times the person viewed that page to then give them a captcha.

I don't understand why you need that. If it's a login area then you should check how many attempts they tried to login but failed.

what I see your trying to count how many times the same person view this one page.

This post has been edited by hockey97: 27 November 2011 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,571
  • Joined: 27-December 08

Re: if statement not working

Posted 27 November 2011 - 05:00 PM

Quote

Dude, your first if statement dosen't have { } those brackets.

If statements don't need braces if they only encapsulate one statement.
Was This Post Helpful? 1
  • +
  • -

#8 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: if statement not working

Posted 27 November 2011 - 05:12 PM

View Postrpgmaker, on 26 November 2011 - 04:17 AM, said:

i start the session. This if statement is in side a config.php file with a session start and a db connect
<?php
	session_start();
	
mysql_connect("localhost", "", "");
mysql_select_db("");



?>


<?php

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
?>



<?php



if ( $_SESSION['views'] > 1 ) {

header( 'Location: http://mysite.net/cap/example_form.php' ) ;
}

?>




Please read the post be for answering next time please I did say in the first post saying i print the session out and the session is being set.
" the views is being set i checked though the print_r function "



Is that one whole file or are they 3 seperate ones? how are they linked?

You have to start the session again because you inbetween you open and closed a php script.

when you open the script again or indicate a start of another php file you need to use that function again. Otherwise you can't call your sessions variables.

View Postmacosxnerd101, on 27 November 2011 - 06:00 PM, said:

Quote

Dude, your first if statement dosen't have { } those brackets.

If statements don't need braces if they only encapsulate one statement.


ya, but is this one file? or are these 3 separate files?

Well, i personally suggest using the brackets.

I am not the only one that thinks this.

http://stackoverflow...nd-curly-braces

This post has been edited by hockey97: 27 November 2011 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,571
  • Joined: 27-December 08

Re: if statement not working

Posted 27 November 2011 - 05:13 PM

Quote

ya, but is this one file? or are these 3 separate files?

How does this have anything to do with whether or not an if statement needs braces?

Quote

I am not the only one that thinks this.

Right, but it is a personal stylistic concern, not a syntactic or standards adherence one.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1