1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I give up

Discussion in 'PHP' started by MrLeN, Apr 6, 2014.

  1. #1
    I give up, what's wrong with this code?

    
    $myFile = 'v/'.$video_id.'.txt';
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "<?php\n";
    fwrite($fh, $stringData);
    $stringData = "/$views = /"0/"\n";
    fwrite($fh, $stringData);
    $stringData = "?>\n";
    fwrite($fh, $stringData);
    fclose($fh);
    
    Code (markup):
    I am trying to write a flat file, and I believe my code is right.

    I can't use:

    $stringData = "$views = "0" \n";

    ..because you can't put a $ inside a quote.

    I can't use:

    $stringData = '$views = "0" \n';

    ..because then the \n wont work (it only works inside a quote).

    So, I have to use:

    $stringData = "/$views = /"0/"\n";

    ..but then I get this error:

    Notice: Undefined variable: views

    PHP should not see it as a variable, that is why I put the bloomin' / on front of it!

    ARGH!

    ..what's going on?

    Maybe I am just tired.
     
    MrLeN, Apr 6, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Using string concatenation:

    
    $stringData = '$views = "0"; ' . "\n";
    
    PHP:
    In PHP the escape character is \ not /, you probably wanted:

    
    $stringData = "\$views = \"0\"; \n";
    
    PHP:
    You can also use the PHP constant for newline:

    
    $stringData = '$views = "0"; ' . PHP_EOL;
    
    PHP:
     
    Last edited: Apr 6, 2014
    ThePHPMaster, Apr 6, 2014 IP
  3. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #3
    ok, I am tired. I was using forward slashes instead of backslashes.

    *uppercuts self*

    Thanks heaps! :)
     
    MrLeN, Apr 6, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    My question would be why the extra variable for nothing? Why are you even using double quotes?

    $myFile = 'v/' . $video_id . '.txt';
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, '<?php $views = "0"; ?>');
    fclose($fh);
    Code (markup):
    NOT that self modifying code is a good idea in the first place. Letting your php write code to be executed is a BIG security hole. (I should know, bit me in the ass about two years ago)
     
    deathshadow, Apr 6, 2014 IP
  5. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I am saving that code to a PHP file, using it as a flatfile database, because I don't know how to make a MySQL database. Basically, I am making a script where each time someone views a page, their view count goes up for their session, which tells me whether I want to reward them.
     
    MrLeN, Apr 6, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    I would HIGHLY suggest storing that as data, not executable code. Executing it is just asking for somewhere sometime having a code execution. Look up fgets and fputs for writing single lines to a file -- since PHP is string neutral you can just read and write it, without executing it via include/require.

    Though a database would be a better choice since it would be faster on doing lookups and you wouldn't have the risk that if you get too many users you hit up against filesystem limits. (since every file on *nix basically sucks down an iNode, and don't even get me STARTED about winblows)
     
    deathshadow, Apr 6, 2014 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #7
    If you are just counting how many pages they've read store it as a session variable or a cookie. Simple, no load on the file system.
     
    sarahk, Apr 6, 2014 IP
  8. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #8
    I know a MySQL database is better, but I get confused. I know how to set a MySQL database up and I know what tables and columns are and generally how they work, but I get stuck when it comes to writing the code to manipulate the database. I've tried to do it before, but I end up with a horrid broken mess that I don't understand. Maybe I can just use the flat file database until I make enough money from the script to have someone make a MySQL version? I don't think the average person using my site would know that I am not using MySQL. However, I do appreciate and agree with your comments.
     
    MrLeN, Apr 6, 2014 IP
  9. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #9
    I want to store it on a file, because other users with different sessions will also be accessing their file, to see if they have a priority over that user. I am really thinking about making a MySQL database, but the way I know how to do it will take me a day. If I try to make a MySQL database I will have to pay someone hundreds of dollars (which I don't have), or sit here for weeks trying ton figure oot how to do it. I want this script done by today, because that's all the time I have. Then, as I work over the coming weeks, I can test whether my idea worked and is worth investing into. I guessz one could say I am making a mock-up version.
     
    MrLeN, Apr 6, 2014 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Most people these don't interact with the database directly - they're all working through the interface provided by their CMS such as joomla or wordpress or a framework. They simplify things and take control of many of the security aspects.

    However, in your case, just use the session or cookies.
     
    sarahk, Apr 6, 2014 IP
  11. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #11
    I am going to use sessions and cookies, but for the views of how many pages they have views, I need to store it on a file in a directory which will be compared to other peoples files. I don't have a CMS installed.
     
    MrLeN, Apr 6, 2014 IP
  12. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #12
    I am contemplating the idea on creating a database. Can anyone give me their two cents on how hard it would be to make:

    Table: username

    Column: views, category

    Basically my script counts how many times a person has loaded a page. It ads 1 view to the "database" each time you view a page.

    This is done for every site visitor, under a username that they entered.

    I have written a script already which looks into the fatfile category of all the usernames, and randomly displays another users details, depending on how many "views" that user has made. Basically, the more a user views other peoples details, the more their own will be shown.

    Is it hard to create a table that allows this, and ads "1" view to the database under their username, each time they load a new page?
     
    MrLeN, Apr 6, 2014 IP
  13. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #13
    ok stuff it, I am going to try and make it with a MySQL database.

    I can already see myself uppercutting myself in 3 hours
     
    MrLeN, Apr 6, 2014 IP
  14. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #14
    Check back in with us if you come unstuck :)
     
    sarahk, Apr 6, 2014 IP
  15. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #15
    MrLeN, Apr 6, 2014 IP
  16. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #16
    ok, I have created he dababase in cPanel
    I seem to have used PHP to execute the action of generating a user table, with the columns of video, category, views:

    
    $con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    // Create table
    $sql="CREATE TABLE Videos(Video CHAR(30),Category CHAR(30),Views INT)";
    
    // Execute query
    if (mysqli_query($con,$sql))
      {
      echo "Table videos created successfully";
      }
    else
      {
      echo "Error creating table: " . mysqli_error($con);
      }
       
    exit;
    
    Code (markup):
    Now I have to figure out how to write some code which says:

    $_SESSION['video'] = "hgjgfjhgfjh"; #this is a youtube video id
    $_SESSION['category'] = "hgjgfjhgfjh"; #this is a youtube video id

    #==on page load

    Insert 1 extra view into the views column of $_SESSION['video'], and add "comedy" into the $_SESSION['category'] column also.

    So I will end up with:

    VIDEOS #table
    hgjgfjhgfjh | 1 | comedy #columns

    If anyone can enlighten me on how to accomplish the above, please do.. because I have pretty much no idea at all.
     
    MrLeN, Apr 6, 2014 IP
  17. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #17
    Hmmm.. just referring to that tutorial page again:

    Primary Keys and Auto Increment Fields
    Each table in a database should have a primary key field.

    A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.

    The following example sets the PID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field:


    
    $sql = "CREATE TABLE Persons
    (
    PID INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(PID),
    FirstName CHAR(15),
    LastName CHAR(15),
    Age INT
    )";
    
    Code (markup):
    I totally don't understand what that is on about.

    Should I add this code to my script (to match my own values)?

    
    $sql = "CREATE TABLE Videos
    (
    PID INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(PID),
    Video CHAR(15),
    Category CHAR(15),
    Views INT
    )";
    
    Code (markup):
    ..I don't really know what it will do, but the "fifth force" is telling me it is necessary (or they wouldn't have it on the tutorial page).
     
    MrLeN, Apr 6, 2014 IP
  18. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #18
    Ok, this code has done the trick:

    
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    
    $sql="INSERT INTO Videos (Video, Category, Views)
    VALUES
    ('$video_id','$video_category',1)";
    
    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }
    echo "1 record added";
    
    mysqli_close($con);
     
    exit;
    
    Code (markup):
    Now, the problem is that if I submit it 3 times, it gets added 3 times to the database.

    ie:

    7DJU3ry0GkQ | comedy | 1
    7DJU3ry0GkQ | comedy | 1
    7DJU3ry0GkQ | comedy | 1


    Now what would prefer is:

    7DJU3ry0GkQ | comedy | 3

    Can anyone help me with that?
     
    MrLeN, Apr 6, 2014 IP
  19. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #19
    MrLeN, Apr 6, 2014 IP
  20. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #20
    Change the create query to remove the other unused fields and making what you need primary:

    
    $sql = "CREATE TABLE IF NOT EXISTS `videos` (
      `Video` char(15) NOT NULL DEFAULT '',
      `Category` char(15) NOT NULL DEFAULT '',
      `Views` int(11) DEFAULT NULL,
      PRIMARY KEY (`Video`,`Category`)
    )";
    
    PHP:
    Then your sql (assuming you are escaping video_id and video_category:

    
    $sql="
    INSERT INTO videos (`Video`, `Category`, `Views`) VALUES ('$video_id', '$video_category', 1) ON DUPLICATE KEY UPDATE `Views` = `Views` + 1";
    
    PHP:
     
    ThePHPMaster, Apr 6, 2014 IP