Jump to content

[SOLVED] Building User Authentication


Trium918

Recommended Posts

I implement a set of 3 pages.

 

This is in output_fns.php the form that

adds the user, and bookmark in a database

table called bookmark.

function display_add_bm_form()
{
  // display the form for people to ener a new bookmark in
?>
<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_bms.php
/* This is the file that <form name=bm_table action="add_bms.php" method=post>
     is submitted to */
<?
require_once("bookmark_fns.php");
session_start();
do_html_header("Adding bookmarks");
check_valid_user();
if (!filled_out($_POST))
{
   echo "You have not filled out the form completely.
         Please try again.";
   display_user_menu();
   do_html_footer();  
   exit;
}
else 
{
   // check URL format
   if (strstr($new_url, "http://")===false)
      $new_url = "http://".$new_url;

   // check URL is valid
   if (@fopen($new_url, "r"))
   {  
     // try to add bm
     if (add_bm($new_url))
       echo "Bookmark added.";
     else
       echo "Could not add bookmark.";
   }
   else
     echo "Not a valid URL.";
}

  // get the bookmarks this user has saved
  if ($url_array = get_user_urls($valid_user));
    display_user_urls($url_array);


   display_user_menu(); 
   do_html_footer();
?>

 

/* This is the function that is called from "add_bms.php"
that add the user and url to the database. My problem is 
that the $valid_user isn't being entered in the database. When 
I do a 'select username from Bookmark' it is blank. The url is ok 
because it is being entered into the database.*/

function add_bm($new_url)
{
  // Add new bookmark to the database

  echo "Attempting to add ".htmlspecialchars($new_url)."<BR>";
  global $valid_user;
  if (!($conn = db_connect()))
    return false;

  // check not a repeat bookmark
  $result = mysql_query("select * from bookmark
                         where username='$valid_user' 
                         and bm_URL='$new_url'");
  if ($result && (mysql_num_rows($result)>0))
    return false;

  // insert the new bookmark
  if (!mysql_query( "insert into bookmark values
                          ('$valid_user', '$new_url')"))
    return false; 

  return true;
} 

 

I was thinking maybe the session_start(); in "add_bms.php"

has something to do with it because the $valid_user has no

value.

Link to comment
https://forums.phpfreaks.com/topic/46356-solved-building-user-authentication/
Share on other sites

session_start() will allow you to store variables in the $_SESSION[] array.  But I don't see that array used in your code.  Variables outside of $_SESSION will not be kept between script runs.

 

That's my point. The first code is a form that's

submitting to add_bms.php. In add_bms.php there

is a function called add_bm($new_url). Inside that

function the username is suppose to be entered into

a the database as well as the bm_url, but the username

isn't going in. Question, should I use $_Session['valid_user']

in add_bms.php or inside the function.

 

Note that the session is started in "add_bms.php":

I implement a set of 3 pages.

 

This is in output_fns.php the form that

adds the user, and bookmark in a database

table called bookmark.

function display_add_bm_form()
{
  // display the form for people to ener a new bookmark in
?>
<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_bms.php
/* This is the file that <form name=bm_table action="add_bms.php" method=post>
     is submitted to */
<?
require_once("bookmark_fns.php");
session_start();
do_html_header("Adding bookmarks");
check_valid_user();
if (!filled_out($_POST))
{
   echo "You have not filled out the form completely.
         Please try again.";
   display_user_menu();
   do_html_footer();  
   exit;
}
else 
{
   // check URL format
   if (strstr($new_url, "http://")===false)
      $new_url = "http://".$new_url;

   // check URL is valid
   if (@fopen($new_url, "r"))
   {  
     // try to add bm
     if (add_bm($new_url))
       echo "Bookmark added.";
     else
       echo "Could not add bookmark.";
   }
   else
     echo "Not a valid URL.";
}

  // get the bookmarks this user has saved
  if ($url_array = get_user_urls($valid_user));
    display_user_urls($url_array);


   display_user_menu(); 
   do_html_footer();
?>

 

/* This is the function that is called from "add_bms.php"
that add the user and url to the database. My problem is 
that the $valid_user isn't being entered in the database. When 
I do a 'select username from Bookmark' it is blank. The url is ok 
because it is being entered into the database.*/

function add_bm($new_url)
{
  // Add new bookmark to the database

  echo "Attempting to add ".htmlspecialchars($new_url)."<BR>";
  global $valid_user;
  if (!($conn = db_connect()))
    return false;

  // check not a repeat bookmark
  $result = mysql_query("select * from bookmark
                         where username='$valid_user' 
                         and bm_URL='$new_url'");
  if ($result && (mysql_num_rows($result)>0))
    return false;

  // insert the new bookmark
  if (!mysql_query( "insert into bookmark values
                          ('$valid_user', '$new_url')"))
    return false; 

  return true;
} 

 

I was thinking maybe the session_start(); in "add_bms.php"

has something to do with it because the $valid_user has no

value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.