Installing Recaptcha. part 1

by Tomrel October 02, 2009 10:41

Look around my blog, you may notice something different about it. In my comments area i have installed a captcha. and in part one of this blog i'mma show u how to install it and get it working.

First of all you need to go to the Recaptcha website and sign up for an account. After that you want to register your website for a key. (or use a global key. i didn't try with the global key.) keep this key handy. you'll need it later.

lets jump into some code. first thing is first, open up your CommentView.aspx

I added this code above all the input boxes for the comments. I got the code from the recaptcha website here.

[code:html]
<script type="text/javascript">
        var RecaptchaOptions = {
            theme : "white"
            };
       </script>
    <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=<Your Public Key>">
    </script>

    <noscript>
        <iframe src="http://api.recaptcha.net/noscript?k=&lt;Your Public Key>" height="300"
            width="500" frameborder="0"></iframe>
        <br/>
        <textarea name="recaptcha_challenge_field" rows="3" cols="40">
   </textarea>
        <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
    </noscript>
[/code]


You remember those keys that I said to keep handy. well replace where it says <Your Public Key> above with.....wait for it....your public key.

now lets jump to the blog.js we are done here.

In this file there is a function called AddComment find that function and go to it. a few lines down into this function you will see several variables being declared. author, email, website, etc at the end of those add a couple of your own. Another thing I did was tack on a couple extra onto that string. that is sent as a callback function.

[code:js]

  var author = nameBox.value;
  var email = emailBox.value;
  var website = websiteBox.value;
  var country = countryDropDown ? countryDropDown.value : "";
  var content = contentBox.value;
  var notify = $("cbNotify").checked;
  var captcha = captchaField.value;
  var reChallenge = $("recaptcha_challenge_field").value;
  var reResponce = $("recaptcha_response_field").value;
 
  var callback = isPreview ? EndShowPreview : AppendComment;
  var argument = author + "-|-" + email + "-|-" + website + "-|-" + country + "-|-" + content + "-|-" + notify + "-|-" + isPreview + "-|-" + captcha + "-|-" + reChallenge + "-|-" + reResponce;
 

[/code]



If you are using blogengine 1.5 you want to use BlogEngine.$() rather then just $(). A couple things by adding the captcha to your page you get a hidden field called recaptcha_challenge_field and a text box called recaptcha_responce_field wich you want to get the values. The next thing I did was send them along with the others to the c# code behind.

so now we move onto the code behind open the  CommentsView.aspx.cs

You want to find a  function called RaiseCallbackEvent(string eventArgument). It'll be in the region called "ICallbackEventHandler Members." In here one of the first things that is done is we split the arguments back to the name, author.....etc etc. So one of the first things we shall do is get our captcha variables as well.


[code:c#]

if (!BlogSettings.Instance.IsCommentsEnabled)
            return;
    string[] args = eventArgument.Split(new string[] { "-|-" }, StringSplitOptions.None);
    string author = args[0];
    string email = args[1];
    string website = args[2];
    string country = args[3];
    string content = args[4];
    bool notify = bool.Parse(args[5]);
        bool isPreview = bool.Parse(args[6]);
        string sentCaptcha = args[7];
        string storedCaptcha = hfCaptcha.Value;
    string reChallenge = args[8];
    string reResponce = args[9];

[/code]



After we do that we can send it off to recaptcha.net to get verified. we accomplish this by sending web request via POST method. The response we get will signify if the captcha was verified or not.


[code:c#]

    //make sure there is something to test before we send.
    if (reResponce.Length > 0)
    {
        //the request we are sending
        WebRequest wr = WebRequest.Create("http://api-verify.recaptcha.net/verify");
        wr.ContentType = "application/x-www-form-urlencoded";
        wr.Method = "POST";
        //the paramaters (privatekey,remoteip,challenge,response)
        string reparam = "privatekey=<Your Private Key>" +
          "&remoteip=" + Request.UserHostAddress +
          "&challenge=" + reChallenge +
          "&response=" + reResponce;
        //turn them into bytes
        byte[] bytes = Encoding.ASCII.GetBytes(reparam);
        Stream os = null;
        wr.ContentLength = bytes.Length;
        os = wr.GetRequestStream();
        //write them to the requeststream
        os.Write(bytes, 0, bytes.Length);

        if (os != null)
        {
            //close the stream
            os.Close();
        }
       
        // get the response
        WebResponse webResponse = wr.GetResponse();
        //check if got response correctly
        if (webResponse == null)
        { return; }
        //read it as stream
        StreamReader sr = new StreamReader(webResponse.GetResponseStream());
        //first line will be true or false
        string strResponce = sr.ReadLine().Trim();
        if (strResponce.ToLower() == "false")
        {
            //false is not verified or error of some sort

            return;
        }
        //if it gets here it's verified
    }
    else
    {
        //nothing sent for captcha not worth progressing forward
        return;
    }

[/code]



Up there around the top of that block of code. there is a place for you to reference your keys and put your private key in. If it gets through here it will continue to post the comment.

 

At this point the code will block comments that the captcha doesn't verify. however it will still tell the user thanks for the comment. and in part 2 we will tackle how to fix that. see you in the next part.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

AJAX | ASP.net | BlogEngine

A Preview

by Tomrel April 23, 2009 07:02

In OpenGL we were sent out on the task to create a game based on the old Qbasic game Gorillas. Here are some screenshots of the first working playable prototype:

 

The Title Screen

 



 

Charector Selection Screen

 

 

 

Game Screens

 






Pause Screen:

 



Special Thanks to:

James Archuleta (www.jamesarchuleta.com)

Luis Valdivia

Ryan Leonski (www.Bit-Chaos.com)

Sek3

for whom the game wouldn't be successful with out them.

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

OpenGL | Gorilla Warfare

tInput

by Tomrel February 25, 2009 13:07

Because of my last two classes, I had a very convenient axis that was quite customizable and any movement wasn't so "choppy." What ever more could I need. I start to move the camera around and realize something. well I realize two things. first camera movement still is choppy. and lastly I can only work one button at a time. I think to myself how lame is that. Going back to XNA I first wonder if I can emulate how they handle keyboard input. I come to the conclusion that I cannot emulate it exactly because when you use XNA everytime you update your scene you have to query the system and get the state of the entire keyboard and see if specific keys are pressed. I couldn't easily do that. So I decided to take a look at what i know. In terms of keys I know 2 things. when the key was pressed and when the key was let go. And I'd like to make the assumption that all the time inbetween the two that the key is down.

I'm realeasing two versions of this class because both glut and wxWidgets handles  mouse stuff slightly different. now lets take a look at how to use these classes.

First just like with anything you must include the proper header file:

#include "tInput.h"

after that you must create an instance of the class

tInput input;

after this is where it gets a bit tricky. at this point you have to set up the events.

wxWidgets:

BEGIN_EVENT_TABLE(tGLCanvas, wxGLCanvas)

    EVT_KEY_DOWN(tGLCanvas::OnKeyDown)

    EVT_KEY_UP(tGLCanvas::OnKeyUp)

END_EVENT_TABLE()

glut:

glutKeyboardFunc(KeyboardState);

glutSpecialFunc(specialKeyboardState);

glutKeyboardUpFunc(KeyboardUpState);

glutSpecialUpFunc(SpecialKUpState);

now inside each individual event you must tell the input class what key was pressed/released

wxWidgets:

void tGLCanvas::OnKeyUp(wxKeyEvent &event)
{
    input.KeyboardUpState(event.GetKeyCode());
}

void tGLCanvas::OnKeyDown(wxKeyEvent &event)
{
    input.KeyboardState(event.GetKeyCode());
}

glut:

void MouseState(int button,int state, int x, int y)
{
    input.MouseState(button,state,x,y);
}


void MouseMotion(int x, int y)
{
    input.MouseState(x,y);
}

void KeyboardState(unsigned char key,int x, int y)
{
    if(menuState == GLUT_MENU_NOT_IN_USE)
        input.KeyboardState(key);
}

void specialKeyboardState(int key, int x, int y)
{
    if(menuState == GLUT_MENU_NOT_IN_USE)
        input.KeyboardState(key);
}

In the glut version I check to see if the menus are in use so that it doesn't register the keyboard states. In wxWidgets I don't need to worry about it because I check when a charecter is entered if it is already registered.

next, only in glut, you need to turn off the keyboard repeat mode:

glutIgnoreKeyRepeat(GLUT_DEVICE_IGNORE_KEY_REPEAT);

and last in your Timer function you can see what keys are currently pressed:

wxWidgets:

if(input.isKeyDown('F'))
{

    ...

}
if(input.isKeyDown(WXK_LEFT))
{

     ...

}

glut:

if(input.isKeyDown('f'))
{

        ...

}

if(input.isKeyDown(GLUT_KEY_LEFT))
{

         ...


}

The rest of this library is going to fucus on glut only. and it deals with Mouse input. first you want to set up your mouse functions.

glutMouseFunc(MouseState);
glutMotionFunc(MouseMotion);
glutPassiveMotionFunc(MouseMotion);

next inside of each mouse function you want to update the tInput class with the mouses current state

void MouseState(int button,int state, int x, int y)
{
    input.MouseState(button,state,x,y);
}
void MouseMotion(int x, int y)
{
    input.MouseState(x,y);
}

now if you want to check if a certant button is down you call:

if(input.isButtonDown(GLUT_LEFT_BUTTON))
{

    ...

}

if you want to get the last change that the mouse had you would call:

elevation += input.GetChangeY() * timer.GetElapsedTime()* Anglemove;
azumuth += input.GetChangeX() * timer.GetElapsedTime()*Anglemove;

note on the above code the GetChangeX and GetChangeY only gets you the change once then the class returns 0 every time after that.

if you want to just get the current x and y position you would call this:

x = input.GetMouseX();

y = input.GetMouseY();

 

 

source code(glut):

tInput.h (3.93 kb)

tInput.cpp (1.85 kb)

 

source code(wxWidgets):

tInput.h (626.00 bytes)

tInput.cpp (1.41 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

"t" Library | OpenGL | wxWidgets

tTime

by Tomrel February 21, 2009 08:51

This class is by far my most useful class in the entire "t" Library. I probably have used this class singlely more then any other class i have written.

My motivation for this class like a lot of my OpenGL classes is that I am trying to emulate the functionality that is already built into the XNA Framework. and that is the GameTime class that is sent into every update and draw in XNA. At the time when I wrote my first OpenGL program I hadn't wrote this class. I had set up my timer correctly but it wasn't exact. as a result the bouncing box in the program looked very "Choppy". this class was written to help eliminate that.

In order to use this class you must first include it:

#include "tTime.h"

then you have to create an instance of it:

tTime timer;

 when the default constructor is called it sets the old time and the current time to what ever the clock() function returns.

next in your timer function the first thing you should do is call the timers' Update function

timer.Update();

now whenever you need to use it you would use the GetElapsedTime() function. here is some psydocode to dementrate this:

pos += vel * timer.GetElapsedTime();

Source Code:

tTime.h (1.54 kb)

tTime.cpp (390.00 bytes)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

"t" Library | OpenGL

tAxis

by Tomrel February 21, 2009 03:03

When I first took OpenGL I was very excited. It was a brand new subject wich I knew very little about. I had worked around with XNA for a while and was really enjoying it. There was one thing about OpenGL wich i thought was very odd; It wasn't very object oriented. Thus it didn't make much since to me at the time. So I made this library of utilities for OpenGL that I coined my "t" Library.

The in one of the first programs we had to create an axis. that stretched from -100 to 100. in both the x and the y direction. I used this class in every program here after. Here i'm going to show you how to use it.

First add the code to the project. (note: the project should be set up for glut. I use that to label my axes)

second where ever you plan on using the tAxis class include the header file:

#include "tAxis.h"

you will need to create an instance of the class:

 tAxis axis;

now lets say we want the axis to span from -150 to 150 have a line width of 2.0f and have x: Red, y: Green and z: Blue. we will have the tick intervals every 25 units apart. and lets turn the labels off so we don't see the X, Y and Z on them. Somewhere before you acually draw the axis (usually in a constructor or where you initialize OpenGL) You can do this:

axis.SetDistance(150.0f);

axis.SetIncrement(25);

axis.SetLineWidth(2.0f);

axis.SetAxisColor(XAXIS,1.0f, 0.0f, 0.0f);

axis.SetAxisColor(YAXIS, 0.0f, 1.0f, 0.0f);

axis.SetAxisColor(ZAXIS, 0.0f, 0.0f, 1.0f);

axis.HideLabel();

  The first line of code in the snippet above will set the distance from the origin to the end of the axes to 150.0 units. next we set the distance between tick marks to 25 units after that we tell this axis to use a wider line(2.0). In the next three lines of code the first argument is the "Axis Index" wich in the header file for this code I set up some #define for you to use. The next 3 arguments are the colors in RGB format between 0.0 and 1.0. that last line of code will hide the labels from apearing at the positive end of the axes.

Lastly whenever you want to draw the axis you will do this:

axis.Draw();

This will draw the axis as you have it set up at the position of (0,0,0) that is at the current top of the matrix stack.

Depending on how you have your camera set up your output should look like this:

 

Source Code: 

tAxis.h (3.06 kb)

tAxis.cpp (2.56 kb)

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

"t" Library | OpenGL

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen | Modified by Mooglegiant
Modified Again by Tomrel

About the AntiBlogger

I am a C# .net programmer. I also do some C++ and also pretty much anything. My programming interests include OpenGL, XNA, and what ever challenges me at the time.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar