How do I use fwrite to write user data?

Discussion in 'PHP' started by Airwalk Enterprise, Inc, Apr 17, 2011.

  1. #1
    So I have this:


    $q_format = file_get_contents('http://www.airwalk-design.com/community/herotozero/write-question.php');
    fwrite($q_write, $q_format);
    fclose($q_write);

    Which writes the file successfully, even the user data. But obviously if I sign out, the user data goes away as the session has been destroyed. How can I get the info to write as text, literally, rather than using $name, etc...here is what I wrote already

    <?php echo "<?php session_start();";?><?php echo "include(\$_SERVER[\"DOCUMENT_ROOT\"].\"/community/herotozero/dontmove-head.php\");?>";?><?php echo "<?php include(\$_SERVER[\"DOCUMENT_ROOT\"].\"/community/herotozero/database.php\");?>";?><?php echo" <?php

    \$name = \$_SESSION['username'];
    \$query = \"SELECT * from category_ask WHERE name='\$name'\";
    \$result = mysql_query(\$query);
    \$row = mysql_fetch_assoc(\$result);

    \$q_title = \$row['q_title'];
    \$category = \$row['category'];
    \$a_info = \$row['a_info'];
    \$s_link = \$row['s_link'];
    \$name = \$row['name'];
    \$entity = \$row['entity'];
    \$city = \$row['city'];
    \$country = \$row['country'];
    \$website = \$row['website'];

    ?>";?>

    <table class="competition" cellspacing="0px" cellpadding="0px">
    <tr>
    <td><div class="image"> <img src="http://www.airwalk-design.com/blog/daniel-schwarz.gif" alt=""/></div>
    <h1> <a><?php echo "<?=\$q_title?>"?>?</a></h1>
    <p>Name: <?php echo "<?=\$name?>"?><br/>
    Entity: <?php echo "<?=\$entity?>"?><br/>
    Location: <?php echo "<?=\$city?>"?>, <?php echo "<?=\$country?>"?><br/>
    Website: <?php echo "<?=\$website?>"?><br/>
    Additional Info: <?php echo "<?=\$s_link?>"?><br/>

    <div class="advertise"> <?php echo "<?=\$a_info?>"?> </div>
    <?php include($_SERVER["DOCUMENT_ROOT"]."/diy/menu-diys.php");?></td>
    </tr>
    </table>
    <?php echo "<?php include(\$_SERVER[\"DOCUMENT_ROOT\"].\"/community/herotozero/dontmove-foot.php\");?>";?>
     
    Airwalk Enterprise, Inc, Apr 17, 2011 IP
  2. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #2
    I don't follow your question. You say it writes to a file but you want it displayed to the user in their browser?

    <?php
    echo $name;
    ?>
    Code (markup):
     
    Alastair Gilfillan, Apr 17, 2011 IP
  3. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, but I want to write a static/information page. If the user signs out, obviously echoing $name won't work, and you'll get a blank space. I want user data to be available to everyone, wether you're signed in or not. For example, getting peoples usernames how they are displayed in this thread next to a forum post.
     
    Airwalk Enterprise, Inc, Apr 17, 2011 IP
  4. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #4
    I have no idea why you aren't using a MySQL database but here's an option if you want to use your files:
    
    user.php?name=airwalk
    
    <?php
    if($_GET['name']){
        if(file_exists("user/".$_GET['name'].".txt")){
            echo file_get_contents("user/".$_GET['name'].".txt");
        }
        else{
            echo $_GET['name']."is not a registered member.";
        }
    }
    else{
        //list all users
    }
    ?>
    Code (markup):
     
    Alastair Gilfillan, Apr 17, 2011 IP
  5. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I never said I wasn't using a database. The problem is that it puts $name from the session I'd to query the database. I want to write user dataand have it there permanently, for others to see. Like a forum. What I'm writint into the next page is a q&a type feaure where people can reply answers :)
     
    Airwalk Enterprise, Inc, Apr 17, 2011 IP
  6. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #6
    Okay. Why not have it query the database each time? If you don't want it live then use your file method. What's the problem?
     
    Alastair Gilfillan, Apr 17, 2011 IP
  7. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm not quite sure what you mean, let me try re-explain.

    ask.php: form which asks a question, with question title, persons name, etc, all from user profile info stored in a database.

    on the next page community/category/questiontitle/ or whatever, it should display the question, the users name, etc. To do this I query'd the database using the name from the session id when the user logs in. what im trying to say, if the user isn't logged in, none of the info is displayed. the fields are blank, naturally, because the session isn't present. You see how in this forum, your name appears next to each post, I want it like that. Not just for me to see, but for everyone to see. I want it live, not private. I want people to be able to reply to the questions nd for them to see who asked the question, etc
     
    Airwalk Enterprise, Inc, Apr 17, 2011 IP
  8. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #8
    You're storing this all in a table of users?

    
    user.php?name=airwalk
    
    <?php
    $name = $_GET['name'];
    $user = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE name='$name'"));
    echo $user['question']."<br>Posted by: ".$user['question'];
    ?>
    Code (markup):
     
    Alastair Gilfillan, Apr 17, 2011 IP
  9. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Isn't that the same thing except by using the GET method? Again, surely, if the user is not logged in, the Posted By field would be blank.
     
    Airwalk Enterprise, Inc, Apr 17, 2011 IP
  10. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #10
    That isn't relying on the session but on the URI visited. Exactly like on this forum - here is your page: /member.php?u=389526

    In addition to my second database-driven example, add:
    
    <?php
    if(!$_GET['name']){
        //list every username with a link to their page like "user.php?name=$namefromdb"
    }
    ?>
    Code (markup):
    This way, if they visit user.php, they see all users and if they have the name query added, it'll only show that single user's page. I hope you understand. I feel you're tackling this the wrong way. Why not look up a simple user registration/display list tutorial or have a look at some open-source forums like PHPBB. Best of luck. :)
     
    Alastair Gilfillan, Apr 17, 2011 IP
  11. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #11
    Airwalk there should be no reason to be using fwrite on a mass scale, its slow and inefficient not to mention a security risk. Alastair is right in what he is saying if you create a page and use query parameters (e.g. users.php?id=1231) then use $var=$_GET['id']; to get that ID and query the database using that. If you want the URL to appear somewhat friendly then you can use Apache's mod_rewrite to get that done.

    Andrew
     
    awood969, Apr 19, 2011 IP
  12. Alastair Gilfillan

    Alastair Gilfillan Active Member

    Messages:
    120
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #12
    Place the following in a file called .htaccess in your root directory.
    RewriteEngine on
    RewriteRule ^user/(.*)$ user.php?name=$1
    Code (markup):
    Then you'll see http://www.example.com/user/airwalk instead of http://www.example.com/user.php?name=airwalk - much more SE friendly. :)
     
    Alastair Gilfillan, Apr 19, 2011 IP