PHP file_exists() not working? - php

I am trying to have this script check if an application exists, and if it does, determine whether it is in the "Accepted" or "Denied" section. The two allowed positions are "Mod" and "Admin".
I have the following code on index.php:
HTML
<!DOCTYPE HTML>
<html>
<body>
<form action="statuscheck.php" method="GET">
<h1>Is this a mod or admin application?</h1>
<input type="radio" name="MOA" value="MOD">Mod</input><br>
<input type="radio" name="MOA" value="ADMIN">Admin</input>
<h1>Put Application ID Below</h1>
<input type="text" name="APPID" placeholder="XXXX-XXXX-XXXX-XXXX" /><br><br>
<input type="submit" value="Check Status" />
</form>
</body>
</html>
This takes the information from the user (to tell statuscheck.php where to go ).
Here is statuscheck.php
PHP
<?php
$APPID = $_GET['APPID'];
$MOA = $_GET['MOA'];
$ROOT = $_SERVER['DOCUMENT_ROOT'];
function modCheck() {
$ACCEPTEDFILEPATH1 = $ROOT . 'public_html/apply/mod- applications/accepted/' . $APPID . '.php';
$DENIEDFILEPATH1 = $ROOT . 'public_html/apply/mod-applications/denied/' . $APPID . '.php';
if (FILE_EXISTS($ACCEPTEDFILEPATH1)) {
echo 'Your moderator application was accepted';
} elseif (FILE_EXISTS($DENIEDFILEPATH1)) {
echo 'Your moderator application was denied';
} else {
echo 'Error : Your application ID is incorrect';
}
}
function adminCheck() {
$ACCEPTEDFILEPATH2 = $ROOT . 'public_html/admin-applications/accepted/' . $APPID . '.php';
$DENIEDFILEPATH2 = $ROOT . 'public_html/admin-applications/accepted/' . $APPID . '.php';
if (FILE_EXISTS($ACCEPTEDFILEPATH2)) {
echo 'Your admin application was accepted';
} elseif (FILE_EXISTS($DENIEDFILEPATH2)) {
echo 'Your admin application was denied';
} else {
echo 'Error : Your application ID is incorrect';
}
}
if ($MOA == "MOD") {
modCheck();
} elseif ($MOA == "ADMIN") {
adminCheck();
} else {
echo 'Please go back and choose mod or admin';
}
I have my PHP tags but they won't work on here so I left them out.
Every time I put a valid ID and select "Mod", the code echos
Error : Your application ID is incorrect.
I do not see anything wrong with my code, and don't understand how it fails the if statements. Can anyone help me ?
EDIT
I have set my php variables at the top of statuscheck.php, they just won't show up in the code on StackOverflow.
They are:
PHP
$APPID = $_GET['APPID'];
$MOA = $_GET['MOA'];
$ROOT = $_SERVER['DOCUMENT_ROOT'];

Related

RabbitMQ: Works in terminal, but not with PHP

for a project I am currently working on I am using rabbitMQ to queue tasks, and send messages. If I run the server and the client from a terminal I will receive data, but if I try to include the php in an index.php that tries to display the into to a web page it does not work. What am I doing wrong, I tried wrapping the testRabbitMQClient.php in a function but this still does not work.What am I missing?
<?php
//index.php
require 'openid.php';
require 'functions.php';
require 'testRabbitMQClient.php';
/*
#connects to my database and checks to make sure its connected.
*/
$apikey="key";
try {
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'http://steamcommunity.com/openid';
header('Location: ' . $openid->authUrl());
}
?>
<h1>Hey ____ thank you for using FOF.com</h1>
<br>
<?php
//showFriends(xxxxxxxxxxxxxxxxx);
//calls function from testRabbitMQClient.php
send("friends");
?>
<form action="?login" method="post">
<input type="image" src="http://cdn.steamcommunity.com/public/images/signinthroughsteam/sits_large_border.png">
</form>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
if($openid->validate()) {
$id = $openid->identity;
// identity is something like: http://steamcommunity.com/openid/id/76561197994761333
// we only care about the unique account ID at the end of the URL.
$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
preg_match($ptn, $id, $matches);
echo "User is logged in (steamID: $matches[1])\n";
}
else
{
echo "User is not logged in.\n";
}
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
?>
#!/usr/bin/php
<?php
//rabbit mq client;
require_once('path.inc');
require_once('get_host_info.inc');
require_once('rabbitMQLib.inc');
function send($type){
$client = new rabbitMQClient("testRabbitMQ.ini","testServer");
if (isset($argv[1]))
{
$msg = $argv[1];
}
else
{
$msg = "test message";
}
$request = array();
$request['type'] = "friends";
$request['username'] = "steve";
$request['password'] = "password";
$request['message'] = $msg;
$response = $client->send_request($request);
//$response = $client->publish($request);
echo "client received response: ".PHP_EOL;
echo $response;
echo "\n\n";
echo $argv[0]." END".PHP_EOL;
}
?>
I figured out my issue! I forgot to enable amqp in apache. I did this by adding the extension=amqp.so to the php.ini file that is located in the apache folder.

OWL Reader::read () error

Im trying to send my search variable to ontology and see if it does belong to any class or subclass as their property. how can i do that?
<?php
$search=$_GET['searchtext'];
if(empty($search)){
echo ("You don't enter anything.");
}
else{
echo ("String you enter is " . $search . " !");
}
require_once "owllib/OWLLib.php";
require_once "$OWLLIB_ROOT/reader/OWLReader.php";
require_once "$OWLLIB_ROOT/memory/OWLMemoryOntology.php";
$reader = new OWLReader();
$ontology = new OWLMemoryOntology();
$reader->read($filename, $ontology);
$class = $ontology->getClass($search);
echo("Ontology output is " .$class);
?>

Not displaying data on PHP file

I am trying to learn some new stuff and always wanted to learn how to make a website with PHP and mysql...
I found this easy tutorial and sample files to play with
http://css-tricks.com/php-for-beginners-building-your-first-simple-cms/
I'm trying to add another table it works in the database but when I try to display it it don't work. Here is the code I got and using:
<?php
class simpleCMS {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT * FROM laptopvoltage ORDER BY created DESC LIMIT 3";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$lvmodel = stripslashes($a['lvmodel']);
$lvmanuf = stripslashes($a['lvmanuf']);
$lvvolt = stripslashes($a['lvvolt']);
$entry_display .= <<<ENTRY_DISPLAY
<div class="post">
<h2>
$lvmodel
</h2>
<p> !!!!!!this dont show upp!!!!!! - - - - >>>>>
$lvmanuf
</p><----------- WHY?
<p>
$lvvolt
</p>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> This Page Is Under Construction </h2>
<p>
No entries have been made on this page.
Please check back soon, or click the
link below to add an entry!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
Add a New Entry
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="lvmodel">Title:lv model</label><br />
<input name="lvmodel" id="lvmodel" type="text" maxlength="150" />
<div class="clear"></div>
<label for="lvmanuf">Title:lv manu</label><br />
<input name="lvmanuf" id="lvmanuf" type="text" maxlength="150" />
<div class="clear"></div>
<label for="lvvolt">Title:lvvolt</label><br />
<input name="lvvolt" id="lvvolt" type="text" maxlength="150" />
<div class="clear"></div>
<input type="submit" value="Create This Entry!" />
</form>
<br />
Back to Home
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['lvmodel'] )
$lvmodel = mysql_real_escape_string($_POST['lvmodel']);
if ( $_POST['lvmanuf'] )
$lvmanuf = mysql_real_escape_string($_POST['lvvolt']);
if ( $_POST['lvvolt'] )
$lvvolt = mysql_real_escape_string($_POST['lvvolt']);
if ( $lvmodel && $lvmanuf && $lvvolt ) {
$created = time();
$sql = "INSERT INTO laptopvoltage VALUES('$lvmodel','$lvmanuf','$lvvolt','$created')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS laptopvoltage (
lvmodel VARCHAR(150),
lvmanuf TEXT,
lvvolt VARCHAR(150),
created VARCHAR(100)
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
it just wont show $lvmanuf. Any help on this would be great as the fields are showing up in my database.
this first file only shows results, if your not to familiar with web logic and design then ill try my best to explain, this first file is called index.php, every website and web-application has a file either call index.html or index.php the reason behind this is that the web server looks for a file named either index.html or index.php and dont misunderstand there are more than just these file types and names a server can start off of its just that these are the most common, since that is out of the way now i will explain the code behind the first file.
as you can see we have set up our basic html document inside and added a script, now the script we made will make the files that are loaded inside the id we specified disappear after a set ammount of seconds, next inside the body of the html we put this code,
<span id="messages">
<?php include "constant.php"; ?>
</span>
this code contains to main players for this script first the span tag with the id attribute tells our javascript the id of the text we want to be invisible after the set amount of seconds, next the
<php include "constant.hpp"; ?>
it includes every thing from the constant.php document we make.
file 1
index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>MySQL Connection test</title>
<script type="text/javascript">
window.onload = function()
{
timedHide(document.getElementById('messages'), 10);
}
function timedHide(element, seconds)
{
if (element) {
setTimeout(function() {
element.style.display = 'none';
}, seconds*1000);
}
}
</script>
</head>
<body>
<span id="messages">
<?php include "constant.php"; ?>
</span>
</body>
</html>
this second file im not going to explain to much about it, since it would make this way to long, but this file is the connection file to the mysql database.
the only part you need to fill in on this is the
$database_ip = ""; //database ip adress goes inside quotes
$database_port = ""; //database port goes inside quotes
$database_name = ""; //database name goes inside quotes
$database_admin_user = ""; //admin username goes inside quotes
$database_admin_pass = ""; //admin password goes inside quotes
this will connect your website to the database.
file 2
constant.php
<?php
$database_ip = ""; //database ip adress goes inside quotes
$database_port = ""; //database port goes inside quotes
$database_name = ""; //database name goes inside quotes
$database_admin_user = ""; //admin username goes inside quotes
$database_admin_pass = ""; //admin password goes inside quotes
//do not modify anything past this point unless you know php well.
$database_link = null;
$database_defaults = array("127.0.0.1","3306","MySQL","root","");
$error_defaults = array("error_no_101" => "required field *IP is empty, using default parameters!",
"error_no_102" => "required field *PORT is empty, using default parameters!",
"error_no_103" => "required field *NAME is empty, using default parameters!",
"error_no_104" => "required field *USER is empty, using default parameters!",
"error_no_105" => "required field *PASS is empty, using default parameters!");
if(empty($database_ip)){
$database_ip = $database_defaults[0];
echo $error_defaults["error_no_101"] . "<br/>";
}
if(empty($database_port)){
$database_port = $database_defaults[1];
echo $error_defaults["error_no_102"] . "<br/>";
}
if(empty($database_name)){
$database_name = $database_defaults[2];
echo $error_defaults["error_no_103"] . "<br/>";
}
if(empty($database_admin_user)){
$database_admin_user = $database_defaults[3];
echo $error_defaults["error_no_104"] . "<br/>";
}
if(empty($database_admin_pass)){
$database_admin_pass = $database_defaults[4];
echo $error_defaults["error_no_105"] . "<br/>";
}
$database_link = mysqli_connect($database_ip, $database_admin_user, $database_admin_pass, $database_name);
if (!$database_link) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
} else {
echo 'Success... ' . mysqli_get_host_info($database_link) . "\n";
}
mysqli_close($database_link);
?>
i put this up to help you fix your code, not to teach you the syntax of the language.
to learn the syntax of php i recommend you go here:
this is the official php website documentation that teach you the correct way to code php,
http://www.php.net/manual/en/langref.php
you could also try this place if you have the money for a subscription:
http://www.lynda.com/MySQL-tutorials/PHP-MySQL-Essential-Training/119003-2.html?srchtrk=index:1%0Alinktypeid:2%0Aq:php%0Apage:1%0As:relevance%0Asa:true%0Aproducttypeid:2
for html you could go to:
http://www.w3schools.com/html/default.asp
you could also try this place if you have the money for a subscription:
http://www.lynda.com/HTML-tutorials/HTML-Essential-Training-2012/99326-2.html

Unable to share PHP variable

I am having trouble sharing a variable value between my main body of code and as function.
For this example, I want to upload an image to the DB with a value which matches the ID of the Place being used.
I currently grab the ID of the place being used from the URL using $_REQUEST and am attempting to write that to a variable 'mid' which I then send to the createthumnail function (contained in functions2.php), but the 'mid' value will not transfer for some reason.
To recreate:
Go to: http://www.students.bl.rdi.co.uk/stu26984/index.php
Login with: Username: Test Password: test123
Go to My Places
Click on WalkaboutSF link
Attempt to upload a file via the 'Choose File' button
Below is the full text of functions2.php
<?php
function createThumbnail2($filename, $mid) {
include 'base.php';
require 'config.php';
if(preg_match('/[.](jpg)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
$ny = floor($oy * ($final_width_of_image / $ox));
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
imagejpeg($nm, $path_to_thumbs_directory . $filename);
// $cookieUserx=$_SESSION['Username'];
// $checkCustidx=mysql_query("SELECT custid AS id from customers WHERE custUsername='".$cookieUserx."';");
// $rx=mysql_fetch_array($checkCustidx);
// $custidx=$rx['id'];
// $updateimage = mysql_query("UPDATE photos SET name = '".$filename."';")or die ('SQL Error: ' . mysql_error());
$updateimage = mysql_query("INSERT INTO photos (NAME, markerid) VALUES('".$filename."', '".$mid."');")or die ('SQL Error: ' . mysql_error());
if($updateimage)
{
header("Location: /WalkaboutSF/viewplace.php?place_id=%20".$mid."");
}
else
{
echo "<h1>Error</h1>";
}
$tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
echo $tn;
}
?>
also here is the code that calls that, from:
http://www.students.bl.rdi.co.uk/stu26984/viewplace.php?place_id=%20133
<div id="insertphoto">
<?php
require 'config.php';
require 'functions2.php';
$place_id = $_REQUEST['place_id'];
$view_place2 = mysql_query("SELECT * FROM markers WHERE id = '$place_id'");
$place2 = mysql_fetch_array($view_place2);
$mid= $place2['id'];
echo $mid;
if(isset($_FILES['fupload'])) {
if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {
$filename = $_FILES['fupload']['name'];
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
createThumbnail2($filename, $mid);
}
}
?>
<h2>Add Photo</h2>
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
<input type="file" name="fupload" />
<input type="submit" value="Go!" />
</form>
</div>
There are a few things that could be going on. Firstly:
$place2 = mysql_fetch_array($view_place2);
$mid= $place2['id'];
echo $mid;
Are you certain that $mid is being set here? Is it possible that the query returned no rows?
If you're sure it has a value here, then check this:
function createThumbnail2($filename, $mid) {
include 'base.php';
require 'config.php';
Are you sure that base.php or config.php are not overwriting the value of $mid? Try changing the code to this:
function createThumbnail2($filename, $mid) {
echo "mid before includes = $mid<br />\n";
include 'base.php';
require 'config.php';
echo "mid after includes = $mid<br />\n";
That will show you if your includes are stomping on your local variables.
edit: I didn't want to sidetrack the question, but your code is open to SQL injection. I could easily wipe your database by calling viewplace.php with a bad value. At the very least, do this:
$place_id = mysql_real_escape_string($_REQUEST['place_id']);
Your insert statement has the same issue. Look into using mysqli or PDO with prepared statements.
edit 2: The reason your code is not working is because there are 2 different HTTP requests working here: one to show the initial page and one to handle the POST to the upload form.
The issue is with the second request (the POST). This code is to blame:
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">
This is NOT passing the ID in $_REQUEST['place_id']. Just add this line below that one:
<input type="hidden" name="place_id" value=<?php echo HtmlspecialChars($_REQUEST['place_id']); ?>" />
This will pass the place_id to the second request, when the form is submitted.

Zend Framework OpenID Consumer

I am using the Zend_OpenId_Consumer to provide OpenID access, the login is working fine, but when I call verify() I am recieving the error
`Wrong openid.return_to 'http://[host]/user/openid' != 'http://[host]/user/openid?[OpenIdResponse]
The issue as far as I can see is that the verify method is comparing the URL without the query part to the entire URL which includes all of the OpenID response information. It gets this url from Zend_OpenId::selfUrl()
I'm using the verify code from the doc pages
$consumer = new Zend_OpenId_Consumer();
if($this->_request->getParam('openid_mode')) {
$id = $this->_request->getParam('openid_claimed_id');
if($this->_request->getParam('openid_mode') == 'id_res') {
if($consumer->verify($this->_request->getParams(),$id)) {
$status = 'VALID ' . $id;
}
else {
$status = 'INVALID ' . $id;
}
}
elseif($this->_request->getParam('openid_mode') == 'cancel') {
$status = 'CANCELLED';
}
}
Am I doing something wrong here?
perhaps this is helpful
Integration with Zend_Controller
Finally a couple of words about
integration into Model-View-Controller
applications: such Zend Framework
applications are implemented using the
Zend_Controller class and they use
objects of the
Zend_Controller_Response_Http class to
prepare HTTP responses and send them
back to the user's web browser.
Zend_OpenId_Consumer doesn't provide
any GUI capabilities but it performs
HTTP redirections on success of
Zend_OpenId_Consumer::login and
Zend_OpenId_Consumer::check. These
redirections may work incorrectly or
not at all if some data was already
sent to the web browser. To properly
perform HTTP redirection in MVC code
the real Zend_Controller_Response_Http
should be sent to
Zend_OpenId_Consumer::login or
Zend_OpenId_Consumer::check as the
last argument.
zend.openid.consumer
strange, i've just tested OpenId_Consumer on my localserver with ZF 1.10.3...
no problem at all
my Action
public function openidAction() {
$this->view->status = "";
if ($this->getRequest()->isPost()) {
$consumer = new Zend_OpenId_Consumer();
if (!$consumer->login($this->getRequest()->getParam('openid_identifier'))) {
$this->view->status = "OpenID login failed.";
}
} else if ($this->getRequest()->getParam('openid_mode')) {
if ($this->getRequest()->getParam('openid_mode') == "id_res") {
$consumer = new Zend_OpenId_Consumer();
if ($consumer->verify($this->getRequest()->getParams(), $id)) {
$this->view->status = "VALID " . htmlspecialchars($id);
} else {
$this->view->status = "INVALID " . htmlspecialchars($id);
}
} else if ($_GET['openid_mode'] == "cancel") {
$this->view->status = "CANCELLED";
}
}
}
my View
<p><?php echo "{$this->status}" ?></p>
<form method="post">
<fieldset>
<legend>OpenID Login</legend>
<input type="text" name="openid_identifier" value=""/>
<input type="submit" name="openid_action" value="login"/>
</fieldset>
</form>

Categories