I'm making an online quiz using AJAX. I've made a registration form for a user to input their details, and the php file returns a username if the details are valid. I'm trying to figure out how to go straight to the main quiz page once the form POST works and be able to display the given username on that main page where I will be using another php file to display the quiz questions. Also I'm using a sample php file for the username return and am unable to view or change anything in that.
-
Please put some code in your question – Blazeag Sep 04 '16 at 08:20
-
1show the username is session. and fetch from the session – Ish Sep 04 '16 at 08:20
-
output the response to the ajax call. – LF00 Sep 04 '16 at 08:25
-
can't you `redirect` the page to the main page? – Saeid Sep 04 '16 at 08:45
-
@sudomakeinstall2 I'm not sure how to do that whilst grabbing the username from the php file – Brittany Sep 04 '16 at 08:46
-
1You can append the username to the page link as a parameter. Something like this: `window.location('yourquizpage.php?user=' + USER_NAME);` And in yourquizpage.php you can parse the username as `$_GET['user']` – Shariq Hasan Khan Sep 04 '16 at 08:53
-
@shariqkhan I only have the link for the php I'm using, I'm unable to edit it. I'm trying to build the html/css/js files around the sample php files – Brittany Sep 04 '16 at 09:04
-
@Brittany - as others have said, you need to post some code for us to understand how your application works. – But those new buttons though.. Sep 04 '16 at 09:12
-
@billynoah I don't have anything to post other than the form (which is incredibly generic, and the main html file which is again just a generic html file at this stage). As said, I don't have access to the php file, just the link to post to – Brittany Sep 04 '16 at 09:15
-
You should probably be using a **session** to store and retrieve credentials but it's a bit weird to develop something like this without having access to the authentication script. that said, you can still develop your own. read here for some ideas: http://stackoverflow.com/questions/10097887/using-sessions-session-variables-in-a-php-login-script – But those new buttons though.. Sep 04 '16 at 09:27
3 Answers
You don't have to use Ajax if you want to go to another page after POST
request
- Post the form.
- Validate data on server.
- If credentials is valid put the username in
cookiesession which means the user has successfully logged in. - redirect the page to the main page.
I strongly recommend you to read more about PHP authentication and use third party libraries if possible.
NOTE: Using an unhashed value as a credential stored in cookie is completely unsafe.
-
am I still able to somehow view the username on the main page with this method? – Brittany Sep 04 '16 at 09:05
-
Yes, the username will be in the cookie.https://en.wikipedia.org/wiki/HTTP_cookie . http://php.net/manual/en/features.cookies.php – Saeid Sep 04 '16 at 09:07
-
storing user credentials in a cookie seems a bit unsafe – But those new buttons though.. Sep 04 '16 at 09:14
-
@billynoah You're right. I'm just trying to convey the idea. That is why I urged him to read more about authentication and use libraries. – Saeid Sep 04 '16 at 09:15
-
1
Apparently you have two pages, one is login form and second is the quiz page. You can save the username to a session(not cookie) from the login page itself and redirect to quiz page upon successful authentication. In quiz page you can easily retain the username from session variable.
The session will long last until a certain period of time that you've mentioned in settings.
You can get this session variable throughout your application.
Using session is pretty straight forward in php, you can read it here on PHP.net
Use password_hash and password_verify (PHP 5 >= 5.5.0, PHP 7 ) with bcrypt for security when saving the passowrds.

- 35,420
- 13
- 53
- 70
-
1[password_hash](https://github.com/ircmaxell/password_compat) for `PHP >= 5.3.7 && < 5.5` – DarkBee Sep 04 '16 at 10:15
-
@DarkBee I copied the structure from php.net. It means works from php 5.5 – Duke Sep 04 '16 at 10:20
-
I know that. The lib I linked is for people who don't have a 5.5 PHP server yet – DarkBee Sep 04 '16 at 11:35
please add your code.
you can use
header()
function in php to redirect your page to quiz page after getting valid data from user.

- 547
- 2
- 9
- 31