Welcome to WebmasterWorld Guest from 34.229.113.106

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get POST to live past registration and into DB if not logged in yet

     
9:44 pm on Oct 24, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:June 16, 2011
posts: 79
votes: 0


Hello everyone,

I'm working on a basic user registration/bookmarking script and am wondering how I can let a user click "Add this to Bookmarks", even when not logged in yet, and if not be presented with a login form and only after they have successfully logged in, have the $_POST be processed and inserted into their user account in the database? I have it all working when the user is logged in, but cant figure out how to make it user friendly if their session expired and still wanted to click the "add to boomkark" links and such.

Example of what I've got so far:

Form that is seen even if user is not logged in:
-as you can see, just a simple form using the method of post.


<form name="bm_table" action="add_bms.php" method="post">
<table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
<tr><td>New BM:</td>
<td><input type="text" name="new_url" value="http://"
size="30" maxlength="255"/></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Add bookmark"/></td></tr>
</table>
</form>

Add bookmark script:


BEGIN PHP

require_once('bookmark_functions.php');
session_start();

//create short variable name
$new_bm = $_POST['new_bm'];

do_html_header('Adding bookmarks');

try {
check_valid_user(); //cant get past this point - code below
if (!filled_out($_POST)) {
throw new Exception('Form not completely filled out.');
}
// check URL format
if (strstr($new_bm, 'http://') === false) {
$new_bm = 'http://'.$new_bm;
}

// check URL is valid
if (!(@fopen($new_bm, 'r'))) {
throw new Exception('Not a valid URL.');
}

// try to add bm
add_bm($new_bm);
echo 'Bookmark added.';

// get the bookmarks this user has saved
if ($url_array = get_user_urls($_SESSION['valid_user'])) {
display_user_urls($url_array);
}
}
catch (Exception $e) {
echo $e->getMessage();
}
display_user_menu();
do_html_footer();

END PHP


Pretty much am stuck after the call check_valid_user() function. I assume i may need to modify it so that the script isn't ended and allow for the $_POST to still live and be inserted after the user has logged in - but have not had any success with it.


Here is what the check_valid_user() looks like:

function check_valid_user() {
// see if somebody is logged in and notify them if not
if (isset($_SESSION['valid_user'])) {
echo "Logged in as ".$_SESSION['valid_user'].".<br />";
} else {
// they are not logged in
do_html_heading('Problem:');
echo 'You are not logged in.<br />';
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}

I was trying to add another condition in the check_valid_user function but no luck.

Summary: How would I go about giving the person the ability to either login or register after clicking a "add bookmark" link so that after they have either registered or logged in, the bookmark would be added to their bookmarks in the DB?

Hope that made sense and thank you in advance for the help.
1:45 am on Oct 25, 2011 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
posts:4061
votes: 0


use sessions

[php.net...]

after they log in, look in the session and see if there's anything there that needs to be saved to the db.
7:58 am on Oct 25, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Jan 4, 2007
posts:221
votes: 0


Either, as mentioned above, use session variables and record after login/registration (you don't need a valid user in order to have a session).

Or, instead of displaying the form on the bookmarking page, redirect to a login/registration page passing the path of the intended page and its variables in the query string. Put them as hidden variables in the form and, once logged in, redirect back to the bookmarking page (will need to use either $_GET or $_REQUEST to pick up the arguments from query string)

Edit - but if you go for the latter approach make sure you validate the data passed in so that people can't use your script to redirect users to other sites.
2:21 am on Oct 26, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:June 16, 2011
posts: 79
votes: 0


Oh okay, I guess that would make sense. Will give it a go tomorrow and see if I can get that to work.

Thanks for the point in the right directions you two!