Jump to content

larry29936

Members
  • Posts

    128
  • Joined

  • Last visited

Everything posted by larry29936

  1. I'm having a problem with PDO. At the top of my index.php, I have the following: <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")): { echo "Failed to connect to database" ; exit; } else: { $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')"; // use exec() because no results are returned $pdo->exec($sql); } endif; //exit(); ?> Further into index.php I have a download section that I want to record in the same table: <?php $files = glob('download/*.iso'); $file = $files[count($files) -1]; $filename = basename($file); $md5file = md5_file($file); ?> <div class="container"> <div class="divL"> <h3>Get the "<?php echo "{$filename}";?>" file (approx. 600MB)</h3> <center> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt=""></a> </center> <?php $down=$filename $sql = "INSERT INTO Downloads (ip_address,filename) VALUES ('$ip','$down')"; <-- parse error here $pdo->exec($sql); ?> I'm getting the following error: Parse error: syntax error, unexpected '$sql' (T_VARIABLE) in /home/foxclone/test.foxclone.com/index.php but don't understand why. Do I not need the $sql line? Thanks in advance, Larry
  2. Now I need to figure out how to update my download table if they download a file. Hold on, I can do it all in the download table. It has the same fields as the login table plus a download name field. If I make that field to allow null, I can use it for logins and downloads. Need to modify the call in the index.php
  3. Yes, I did. It told me about the invalid credentials.
  4. Got it fixed. While I can connect to the database thru phpmyadmin using a uid of larry locally, to connect from PDO_Connect_Select I needed to use foxclone_larry. Thanks for all your help and putting up with a newbie.
  5. I verified that the database name is being passed correctly by using an echo in PDO_Connection_Select.php. uid and pswd are the same both locally and on the host server. The error is coming from the catch in PDO_Connection_Select.php, not index.php
  6. Here's a link to a screenshot of the error when run on the host: [/url]. Here's the code I'm running that works locally: Top of index.php: <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")): { echo "Failed to connect to database" ; // exit? } else: { echo "Connected to database"; $sql = "INSERT INTO `Logins` (`ip_address`) VALUES ('$ip')"; // use exec() because no results are returned $pdo->exec($sql); } endif; //exit(); ?> <!DOCTYPE html> PDO_Connection_Select.php: <?php function PDOConnect($l_dbname='', $l_msg=null, $l_options=null) { if ($l_options == null) { // set my default options $l_options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); } if ($l_dbname == null) $host="mysql:host=localhost;charset=utf8"; else $host="mysql:host=localhost;dbname=$l_dbname;charset=utf8"; $uid = "xxxxx"; $pswd = "yyyyyy"; try { $mysql = new PDO($host, $uid, $pswd, $l_options); } catch (PDOException $e) { if (strtoupper($l_msg) == "SHOWMSG") echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); else echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO. Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'showmsg')"; return false; } if (!$mysql) return false; else // all worked - return handle to pdo connection. return $mysql; } ?> The structure is the same both locally and on the web host.
  7. The code runs fine locally but fails to pass the db name to PDO_Connection_Select when run on the webhost server. Waiting for feedback from the webhost.
  8. The catch was copied from your second example near the bottom of page 1. I think it should be like this: //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) { echo "Failed to connect to database" ; // exit? } elseif ($pdo = PDOConnect("foxclone_data")) { $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip'); // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } //catch(PDOException $e) else { echo "Query failed to run properly: $sql <br><br>" . $e->getMessage(); } exit(); ?>
  9. // index.php <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data")) <---- was missing closing quote per php syntax checker { echo "Failed to connect to database" ; <----- was missing ; per php syntax checker // exit? } else { $sql = "INSERT INTO `LOGIN` (`id`, `when_login`, `ip_address`) VALUES (NULL, current_timestamp(), '$ip'); <-- syntax copied from phpmyadmin // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } catch(PDOException $e) <----- current error { echo "Query failed to run properly: $sql <br><br>" . $e->getMessage(); } exit(); ?>
  10. Done. Further question re: error in index.php. PHP Syntax Check: Parse error: syntax error, unexpected 'catch' (T_CATCH), expecting end of file in your code on line 26 catch(PDOException $e)
  11. @ginerjm - A couple of questions for you after you review my PDO_Connection_Select.php and index.php //PDO_Connection_Select.php <?php function PDOConnect($l_dbname, $l_msg=null, $l_options=null) { if ($l_options == null) { // set my default options $l_options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); } if ($l_dbname == null) // <---------------- In my case, this is resolving to true. Do I need to set l_dbname above here? $host="mysql:host=localhost;charset=utf8"; else $host="mysql:host=localhost;dbname='foxclone_data';charset=utf8"; $uid = "xxxxx"; $pswd = "yyyyy"; try { $mysql = new PDO($host, $uid, $pswd, $l_options); } catch (PDOException $e) { if (strtoupper($l_msg) == "SHOWMSG") echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); else echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO. Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'showmsg')"; return false; } if (!$mysql) return false; else // all worked - return handle to pdo connection. return $mysql; } ?> // index.php <?php session_start(); // start of script every time. // setup a path for all of your canned php scripts $php_scripts = '/home/foxclone/php/'; // a folder above the web accessible tree // load the pdo connection module require $php_scripts . 'PDO_Connection_Select.php'; require $php_scripts . 'GetUserIpAddr.php'; //******************************* // Begin the script here $ip = GetUserIpAddr(); if (!$pdo = PDOConnect("foxclone_data") { echo "Failed to connect to database" //<-------------------------------- Errors here ("Parse error: syntax error, unexpected 'echo' (T_ECHO) in /home/foxclone/test.foxclone.com/index.php on line 15") // exit? } else { $sql = "INSERT INTO LOGIN (ip_address) VALUES ('$ip2' )"; // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } catch(PDOException $e) { echo "Query failed to run properly: $sql <br><br>" . $e->getMessage(); } exit(); ?> Now for the questions. Why isn't the db_name getting passed to PDO_Connection_Select.php? Why the error on the echo of the failure message in index.php?
  12. Based on what you provided, here's the start of my index.php: <?php $ip2 = GetUserIpAddr(); function GetUserIpAddr() { if(!empty($_SERVER['HTTP_CLIENT_IP'])) { //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } function PDOConnect($l_dbname="foxclone_data", $l_msg=null, $l_options=null) { if ($l_options == null) { // set my default options $l_options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_FOUND_ROWS => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); } if ($l_dbname == null) $host="mysql:host=localhost;charset=utf8"; else $host="mysql:host=localhost;dbname=$l_dbname;charset=utf8"; $uid = "xxxxxxx"; $pswd = "yyyyyyyyy"; try { $mysql = new PDO($host, $uid, $pswd, $l_options); } catch (PDOException $e) { if (strtoupper($l_msg) == "SHOWMSG") echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); else echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO. Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'showmsg')"; return false; } if (!$mysql) return false; else // all worked - return handle to pdo connection. return $mysql; } $ip2 = string($ip) session start(); if (!$pdo = PDOConnect("foxclone_data") { echo "Failed to connect to database" // exit? } // proceed with $pdo as the handle to all database interactions else { $sql = "INSERT INTO LOGIN (ip_address) VALUES ('$ip2' )"; // use exec() because no results are returned $pdo->exec($sql); echo "New record created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } ?> <!DOCTYPE html> How many mistakes did I make? I did add the indicated entries in php.ini.
  13. Thanks for your help and suggestions. I'll do development and testing on my local server before deploying to the website.
  14. Is this anywhere near being correct? <?php session start(); $servername = "localhost"; $username = "xxxxxxx"; $password = "yyyyyyy"; $dbname = "foxclone_data"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO LOGIN (when_login, ip_address) VALUES (now(), '$ip' )"; // use exec() because no results are returned $conn->exec($sql); echo "New record created successfully"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; $ip = GetUserIpAddr(); function GetUserIpAddr() { if(!empty($_SERVER['HTTP_CLIENT_IP'])) { //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } ?> <!DOCTYPE html> etc, etc
  15. @ginerjm , I don't understand how $q is being executed in your example. I also don't understand how it knows how the database connection is made without the mysql_connect parameter. I'd appreciate an explanation.
  16. Actually, here's what I think it should be: <?php $con = mysql_connect("localhost","xxxxx","yyyyyyyyy"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("foxclone_data", $con); function getUserIpAddr(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } session_start() INSERT INTO LOGIN (when_login, ip_address) VALUES (now(), $ip ); ?> <!DOCTYPE html> What do you think? My next question is, do I keep the session open so I can capture any download information or close it and open a new session for downloads?
  17. So, like this? <?php session_start() function getUserIpAddr(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } INSERT INTO LOGIN (session_id, when_login, ip_address) VALUES (session_id(), now(), $ip ); ?> <!DOCTYPE html>
  18. Does this look right? <?php session_start() function getUserIpAddr(){ if(!empty($_SERVER['HTTP_CLIENT_IP'])){ //ip from share internet $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //ip pass from proxy $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; }else{ $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } $session = array[ses] $ses[] = $session_id; $ses[] = $ip; INSERT INTO LOGIN (session_id, when_login, ip_address) VALUES ($ses[0], now(), ses[1] ); ?> <!DOCTYPE html>
  19. What data type should I use for ip_address? I guess I should convert to a string and store as a varchar.
  20. Thank you VERY much. 20 years ago, I was writing front-ends for industrial databases. Now you've triggered memories of how to build sql queries, joins, foreign-keys, etc. You also provided the knowledge of how to use the data on the website. Time for a little study before proceeding. Thanks again, Larry BTW-How do I mark this as solved?
  21. This is my first stint as a "webmaster" and in many ways, ignorant. I'm learning as I go. So, I should go to my web host and set-up a mysql db for two single field tables, one for website hits and one for downloads? EDIT: If I capture the url for both, I can join the tables and know who visited AND downloaded. (It's been 20 years since I worked with any databases)
  22. The only thing I need to store is a number. Couldn't that be stored in a text file? When the download button is clicked, read the value in the text file, increment +1, and write it back to the text file? A read of the text file and display in text box.
  23. I have a download section in my index.php that I'd like to add a download counter to but have no idea how to accomplish this. The existing code works perfectly but I'd like to know how many times the file is downloaded. Would prefer not to use a database. <!-- DOWNLOAD --> <div id="download" class="stylized"> <div "myform"> <div class="container"> <div class="row"> <div class="download"> <br /><br> <h1><center>FoxClone Download Page</center></h1> <?php $files = glob('download/*.iso'); $file = $files[count($files) -1]; $info = pathinfo($file); $filename = basename($file); $filename = ltrim($filename,'/'); $md5file = md5_file($file); ?> <div class="container"> <div class="divL"> <h3>Get the "<?php echo "{$filename}";?>" file (approx. 600MB)</h3> <center> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt=""></a> </center><br /> <h3 style="margin-bottom: 0.5rem;">The MD5sum for "<?php echo "{$filename}";?>" is "<?php echo "{$md5file}";?> Thanks in advance, Larry
  24. Since I installed apache2, php, and phpmyadmin on my linux machine when I click on a .php file it downloads instead of opening in a web browser. It didn't do this prior to the install, so I suspect it's a config error. Can someone help me on this? Thanks in advance, Larry
  25. Problem resolved. <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $info = pathinfo($file); $filename = basename($file); $filename = ltrim($filename,'/'); ?>
×
×
  • 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.