Jump to content

Mad programmer

Members
  • Posts

    26
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by Mad programmer

  1. For the loop I'd recommend to use a nested foreach.(on the array you've used in your print_r) The first foreach kan be used like:

     

    Foreach($array as $key => $value) { ... }

     

    Where $key is example.com and value are its children. So then do another foreach inside this one with the $value as an array. That should work!

  2. Woa woa hold there :D you DO know how to use a variable in a string right?

     

    Take my modified example from above and BEFORE that do this; $id = $_SESSION['userid']; bindParam is not working with query but only with prepared statements.. the fetch function is only taking pdo arguments such as PDO::FETCH_ASSOC.

     

    My modified example is all you need, you're combining prepared pdo wirh normal query statements and that wont work.

  3. That is correct, however PDO does not always require that much lines of code. The prepare functions are for safety purposes. It would be good practice to put these to use.

     

    But in your case you typecast to an int by using intval which may also be passed directly to a sql query. You can do this with PDO like so:

     

    (Example taken from php.net/pdo and modified a bit)

     

    <?PHP

    $users = $pdo->query("SELECT * FROM users WHERE id = {$id}")->fetch();

    ?>

     

    Sorry for not using codetags but I am on my cell phone right now.

  4. If you by different syntax mean the way of constructing a query in PDO then no. Except for binding the parameters. This is because pdo sends the query to the mysql server and afterwards your binded parameters.

     

    As of php 5.5 the mysql_* functions will be removed(I think) and you will need to use pdo or the mysqli functions for your scripts.

     

    MySQL itself cannot be shutdown since it is just a database software package, the interfaces for php are a different story :)

  5. Well MySQL is the language for the database itself while PDO is an abstraction layer for the database.

     

    You can still use variables in a string like you did right there, however the bindParam also makes sure your data is safe and it protects against sql injections.

  6. True, but I was talking about execute and not exec ;)

    This is what the docs say:

    PDO::exec() executes an SQL statement in a single function call, returning the number of rows affected by the statement.


    You do NOT want the affected rows but you want to retrieve the rows themself which is done like this:

     

    <?php
    $id = 441;
    		
    $query = $db->prepare('SELECT * FROM users WHERE id = :id'); // You first PREPARE the query
    $query->bindParam(':id', $id);	 // You bind the required parameters for your query
    $query->execute(); // This sends the query to the SQL server
    
    $user = $query->fetch(PDO::FETCH_ASSOC); // Specify the PDO::FETCH_ASSOC to fetch the data as an associative array (not required)
    
    echo '<pre>'; // I always PRE before I print an array which makes it more readable in the browser
    print_r($user); // This contains your fetched row from the sql server
    
    ?>
    

    Please compare the above piece of code to what you have written and note the differences.

  7. You can use variables :) That is where the prepare function is for. Like so:

     

    $query = $db->prepare('SELECT *FROM pages WHERE offline = :offline');

    $query->bindParam(':offline', $offlineVariable);

     

    Prepare does as the name is called, it prepares the data for the sql server. Such as escaping the data etc.

  8. Hello,

     

    I'd like to suggest that you look into pdo's prepare function. First you build up your query with pdo prepare and then execute it. After that you may return the data in this way:

     

    $pdo->fetchAll(PDO::FETCH_ASSOC);

     

    Which returns all data as an associative array. You might find this link usefull ;)

     

    http://php.net/pdo

  9. Hello,

    As an framework I'd like to suggest symfony2, due to its modern standard and great possibilities. It features a build in authentication system. As for the pagination there are modules for that which can easily be installed in your application. I thought the name was KnpPaginatorBundle.

    As for the database symfony2 comes with doctrine 2 with is an powerful orm/dbal. You can abstract your database work into php classes wich is rather nice. The documentation can get you started on the framework, it is not complete. But it does explain things pretty well (in my opinion)

  10. Hello again!

     

    I have tested your code on my local server and I got several errors, with these errors the problem was quite easy to find.

    If you take a look at the code you seem to have made a little mistake ;P

     

    $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
    if (!in_array($page, $files)){
    require_once('pages/' . $_GET['page'] . ".php");
    }
    

     

    The problem is on the 3rd line above, on the first line you set the variable $page to intro if $_GET['page'] is not set else you just use the $_GET variable.

    However on the 3rd line you are using the $_GET variable instead of the $page variable.

    This should work for you:

     

    $page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
    if (!in_array($page, $files)){
    require_once('pages/' . [color=#ff0000]$page[/color] . ".php");
    }
    

  11. @Freid001:

     

    Here is an adjusted version of my demo code, this one contains the timestamp as I've suggested.

    If the session has been set 30 seconds ago the script destroys the session and allowing the user on the page again. (Right after that the timeout is set to 30 again);

     

    <?php
    session_start();
    //  This is the timeout that the session needs to be checked against (seconds)
    $timeout = 30;
    
    //	Allows us to manually unset the session to test our code 
    if (isset($_GET['delete'])) {
    unset($_SESSION['check']);
    exit();
    }
    
    //	If our session is not set then create it with its values to false
    if (!isset($_SESSION['check'])) {
    $_SESSION['check'] = array(
    	'set' => false,
    	'time' => time()
    );
    }
    
    if ($_SESSION['check']['set'] == true) {
    //	Check our current time against our timeout
    if (time() <= $_SESSION['check']['time'] + $timeout ) {
    	echo 'You should not be here!<br /><br>Timespan: ';
    	echo time() - $_SESSION['check']['time'];
    	exit();
    } else { 
    	//	Session is older than 30 seconds, you might unset it..
    	unset($_SESSION['check']);
    }
    }
    
    //	The user may view the page but we set our session for control purposes!
    $_SESSION['check'] = array(
    'set' => true,
    'time' => time()
    );
    echo 'welcome!';
    

     

    Let me know if it works for you :)

     

    @xyph:

    I did not mean to say that if its possible to see where the request came from, Take a look at my newly posted code that's how I've meant it all along.

     

  12. @xyph:

    I know it affects the current tab as well if you refresh it (Should've mentioned that), this can be fixed by adding some sort of flood control.

    If you set the session you can add in a current timestamp, and in the session check you can check against the session and the timestamp.

    If the timestamp is 30 seconds old then unset the session, with this solution you can prevent spamming a little.

     

    @Freid001:

    Where you unsetting the session when you output an error to the viewer? However my method can be used however it also has its disadvantages like Xyph said.

    You can try adding in a time based mechanism like I said, It doesn't matter how many tabs someone has open the code will always be checked against the session.

    The real problem is when someone reloads the page, then they have to wait :)

  13. @xyph:

     

    I know they persist, however if you use my approach and open the page in a new tab the session is set.

    Thus further code execution can be prevent, there is indeed no way to tell if it is a new tab.

     

    If you try my script you can see what I mean

     

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