Jump to content

[SOLVED] Flat file and passing data


Schlo_50

Recommended Posts

Hey guys,

 

I am currently working on a project whereby users login to my website (which is working) and then using a form add new links/tabs to their navigation bar (which i need help developing). So for instance if the user had 'home/search engines/favourites' as links on their navigation bar and they wanted to add auction sites as a group title they fill out the form which wants the name of the link and the URL it goes to.

 

So far i can make the form send the link name and URL to my flat file database (usertabs.txt) but i also need the form to once submitted automatically insert the users username before the link and URL to make their new link unique to them. So in the flat file the structure will be:

 

username|Link name|URL

 

The usernames etc are stored in a separate .txt file.

I have this code so far which is the basis for uploading new links to a users navigation bar. $userid is the variable i want to be associated with the user who is logged on.

 

function addtab($name,$link){
if ($_POST['submit'] == "submit")
{
$file_name = "usertabs.txt";
$open_file = fopen($file_name, "a+");
$file_contents= $_SESSION['$userid)'] . '|' . $_POST['name'] . '|' . $_POST['link'] ."\n";
fwrite($open_file, $file_contents);
fclose($open_file);
echo "New Tab successfully uploaded";
}
}

 

This is how i check user existance if this helps?

 

$pfile = fopen("users.txt","r");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {

 

I am really stuck and i just don't know how to make the upload a new link part add the users username if they are logged in.

 

Anyone any suggestions or help?

 

Thanks!

 

Link to comment
Share on other sites

Are your users logged in using sessions? If so, simply store the users username in the $_SESSION array ($_SESSION['username']) and use that.

 

$file_contents= $_SESSION['username'] . '|' . $_POST['name'] . '|' . $_POST['link'] ."\n";

Link to comment
Share on other sites

Yeah--->

 

<?php
session_start();
// REGISTRATION SECTION
function registerUser($user,$pass1,$pass2){
$errorText = '';
// Check passwords
if ($pass1 != $pass2) $errorText = "The passwords you have entered do not match!";
elseif (strlen($pass1) < 6) $errorText = "Your chosen password is too short. Passwords must be at least 6 characters in length.";

// Check user existance	
$pfile = fopen("users.txt","a+");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            $errorText = "The user name you chose has been taken!";
            break;
        }
    }

    // Store user data
    if ($errorText == ''){
	// Convert the password to MD5
	$userpass = md5($pass1);
    	
	fwrite($pfile, "\r\n$user:$userpass");
    }
    
    fclose($pfile);


return $errorText;
}

function loginUser($user,$pass){
$errorText = '';
$validUser = false;

// Check user existance	
$pfile = fopen("users.txt","r");
    rewind($pfile);

    while (!feof($pfile)) {
        $line = fgets($pfile);
        $tmp = explode(':', $line);
        if ($tmp[0] == $user) {
            // User exists, check password
            if (trim($tmp[1]) == trim(md5($pass))){
            	$validUser= true;
            	$_SESSION['userName'] = $user;
            }
            break;
        }
    }
    fclose($pfile);

    if ($validUser != true) $errorText = "Invalid username or password!";
    
    if ($validUser == true) $_SESSION['validUser'] = true;
    else $_SESSION['validUser'] = false;

return $errorText;	
}
// Logout the user
function logoutUser(){
unset($_SESSION['validUser']);
unset($_SESSION['userName']);
}
// Check to see if user is logged in
function checkUser(){
if ((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
	header('Location: login.php');
}
}
//get customer id and add as the key in usertabs.txt
function userid($userid){

$file = file("users.txt"); //puts each line of the file into an array

foreach($file as $key => $val){ //loop through array

  $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by |

  $userid = $data[$key][0];
  }
}
//add new tabs
function addtab($name,$link){
if ($_POST['submit'] == "submit")
{
$file_name = "usertabs.txt";
$open_file = fopen($file_name, "a+");
$file_contents= $_SESSION['userid($userid)'] . '|' . $_POST['name'] . '|' . $_POST['link'] ."\n";
fwrite($open_file, $file_contents);
fclose($open_file);
echo "New Tab successfully uploaded";
}
}
//display new tabs
function gettabs($eventmonth){

$file = file("usertabs.txt"); //puts each line of the file into an array

foreach($file as $key => $val){ //loop through array

  $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by |

  $userid = $data[$key][0];
  $name = $data[$key][1];
  $link = $data[$key][2];

   $output = "<a href='$link'>$name</a><br>\n"; // NTS need to output link name with underlying url..
   print $output;
  }
}
//Using the function (gettabs()
?>

 

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.