Jump to content

[SOLVED] Logged in users, sessions and flat file .txt


Schlo_50

Recommended Posts

Hey guys,

 

I am currently working on a project whereby users login to my website and then using a form add new quick links/tabs to their navigation bar. 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 link they fill out the form which then posts the data into a flat file database. (usertabs.txt)

 

So far i can make the form send the link name, URL and attach the username of the person adding links so to make a id for the link name and URL. So in the flat file the structure is presently:

 

username(unique)|Link name|URL

schlo_50|Google|http://www.google.com

 

What i want to do is write some code to say, 'when Joe Bloggs is logged in, display all links uploaded by him by searching usertabs.txt and outputting any link name and URL that follows his username.'

 

So far i have:

 

 

function displaytab(){

$usertabs = $_SESSION['userName'];

$file = file("usertabs.txt");
foreach($file as $key => $val){
$data[$key] = explode("|", $val); 

  $user = $data[$key][0];
  $link = $data[$key][1];
  $url = $data[$key][2];
}

if ($usertabs == $user){
echo $link, $url;
}
}

 

This is what i have so far, it's just i need something that works better with sessions.. If i log in with one user and upload a new link the script works but if i then log out and login with a different username the links aren't outputted.

 

I've designed that code to search the whole text file for any entries matching the users username and output it but it doesn't do it very well. Help anyone? Maybe its my file search method?

 

Help please,

Regards

 

Link to comment
Share on other sites

I assume you have session_start() at the top of the page? Try...

 

<?php

function displaytab(){

  $lines = file("usertabs.txt");
  foreach ($lines as $line) {
    $data = explode("|", $line); 
    if (current($data) == $_SESSION['userName']) {
      echo next($data), next($data);
    }
  }
}

?>

Link to comment
Share on other sites

I did that which worked perfectly. The thing now, is that i need to arrange any information sent to my .txt file so that it falls on a new line.

 

The code i am using to upload file contents so far is:

 

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

 

The code above inputs data in rows separated by '|' but i want new lines. I have a picture to illustrate what i want:

 

exnl3.jpg

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.