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.