Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- file1.php
- require_once(ABSPATH.'wp-config.php' );
- require_once(ABSPATH.'functions.php' );
- $domain = $_SERVER['HTTP_HOST'];
- $uri = parse_url($_SERVER['HTTP_REFERER']);
- $r_domain = $uri['host'];
- add_action( 'admin_post_table', 'table' );
- function table() {
- if( ! check_ajax_referer( 'nonce', 'securitycheck' ) ) {
- wp_send_json_error('Security check failed');
- }
- if( ! empty($_POST['submission']) ) {
- wp_send_json_error('Security check failed, regular form entries only!');
- }
- if ( $domain == $r_domain ) {
- $conn = f_sqlConnect();
- if ( !f_tableExists($conn, $table) ) {
- try {
- $sql = "CREATE TABLE $table (
- ID int NOT NULL AUTO_INCREMENT,
- PRIMARY KEY(ID),
- ip int NOT NULL
- )";
- $result = $conn->exec($sql);
- } catch(PDOException $e) {
- echo $e->getMessage(); //Remove or change message in production code
- echo "Create table error";
- }
- }
- functions.php
- /*Connects to and selects the specified database with the specified user.*/
- function f_sqlConnect() {
- $host = DB_HOST;
- $db = DB_NAME;
- try {
- $conn = new PDO("mysql:host=$host;dbname=$db", DB_USER, DB_PASSWORD);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo "Connected successfully";
- }
- Catch(PDOException $e)
- {
- echo "Connection failed: " . $e->getMessage();
- }
- }
- /*Checks to see if the specified table exists and if it doesn't, creates it.*/
- function f_tableExists($conn, $table) {
- //$table = preg_replace('/[^da-z_]/i', '', $table);
- // Try a select statement against the table
- // Run it in try/catch in case PDO is in ERRMODE_EXCEPTION.
- try {
- $query = "SELECT 1 FROM $table LIMIT 1";
- $result = $conn->exec($query);
- } catch (Exception $e) {
- echo "We got an exception == table not found";
- return FALSE;
- }
- echo "Result is either boolean FALSE (no table found) or PDOStatement Object (table found)";
- return $result !== FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement