0

I'm writing a log-in system using PHP, mySQL and Javascript. My site is effectively a 1 page app written in javascript - only 1 html page. All interaction and navigation is done through javascript.

When a user registers, I create their record in the db with a 32 digit key in the activation column. I e-mail this to the registrant as an activation link. This takes them to a php file that activates their account (or not if there is an error). All well and good.

After activation (or error) I could take them to an html page (e.g. header('somesite.com/success.html') telling them whether their account is activated or not but I'd much rather take them back to a specific function in my 1 page javascript site. How can I do this?

I can take them to the site but how do I pass a message from my php re-direct to the site so it knows whether to display a success or error message?

Do I put it in the URL of the re-direct e.g. http://somesite.com?activation=success? If so, how do I get this variable into my javascript?

I could set a session variable from the php activation script and check it in my code but that seems very clumsy.

I could set a hash in the URL and pick that up in the code but I avoid hash navigation if I can. Any ideas on the method to achieve this?

Final answer from the help below and elsewhere on the site:

function getURLParameter(name) {
return decodeURIComponent(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}

then a redirect on registration such as somesite.com?email=somebody%40else@somewhere.com&key=7da93f78cb4942555863c161f50f258d

I can get these variables as simply as getURLParameter('email') and getURLParameter('key')

Thanks for everyone's help. Gotta love this site

George
  • 36,413
  • 9
  • 66
  • 103
Kage
  • 81
  • 1
  • 2
  • 11

3 Answers3

1

since you have everything in your one page app, you could use that for the activasion as well -- have the activation link go to http://homesite.com?activation=<32characterkey> and when your app detects the GET param, use AJAX to call the PHP activation, and notify the user of the outcome.

Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
  • I like the sound of this approach. I'm going to sound stupid but I don't know how to get the variable into my javascript code from the URL. I know it's a basic question but it's not something I've ever done. – Kage Feb 28 '13 at 10:35
1

You can get the variables from the URL with Javascript:

I actually asked a similar question (I can't find it) about getting URL variables with Javascript, and somebody very helpfully gave me this function:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {vars[key] = value});
    return vars;
}

So to obtain a GET variable called 'activation' you would simple call the function like this:

getUrlVars()['activation']
George
  • 36,413
  • 9
  • 66
  • 103
  • Tried this but can't get it to work. I included the function then put this as my URL: http://localhost/newproject/index.html?activation=hello but when I try to get the variable it is showing as undefined – Kage Feb 28 '13 at 10:42
  • Got it. It's coming back as an object - can read it if I do this alert (getUrlVars().activation); – Kage Feb 28 '13 at 10:47
  • I've just tested it and it is working perfectly for me, are you sure you are calling the function properly? – George Feb 28 '13 at 10:48
0

You should use to AJAX to login and call your functions in the AJAX success/error callbacks.

You can do that easily with jQuery $.ajax() function

http://api.jquery.com/jQuery.ajax/

dlock
  • 9,447
  • 9
  • 47
  • 67
  • Using AJAX all the time in my code with success/error callbacks but the PHP page I'me sending the registrant to is just a basic script and I'm re-directing them back to the site. My question is how to get a message back to my code from a PHP re-direct – Kage Feb 28 '13 at 10:44
  • Your best bet is to hash a message in the url and get it from javascript using `window.location.hash`. Then you can call your function based on the string you get from the hash. And I don't think there are other ways to do that. – dlock Feb 28 '13 at 10:46
  • Thanks but trying to avoid hash. F4r-20 seems to have the answer for me. Thanks for help – Kage Feb 28 '13 at 10:52