[SOLVED] code doesn't execute...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

[SOLVED] code doesn't execute...

Post by arukomp »

Hi everyone

I'm having a problem again :( My code doesn't execute...

here's first part of the code in the first page:

Code: Select all

function otherprogram($pid, $id){
	$sql = "SELECT * FROM `user` WHERE id='$id'";
	$result = mysql_query($sql);
	$array = mysql_fetch_array($result);
	$name = $array['name'];
	$egold = $array['egold'];
	$email = $array['email'];
	global $url;
	global $key;
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url . '/insertnewuser.php?key='.$key.'&p='.$pid.'&name='.$name.'&email='.$email.'&egold='.$egold);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $a = curl_exec($ch);
    if (!$a) {
		curl_exec($ch);
	}
    curl_close($ch);
}
And here's second part of the code in insertnewuser.php page:

Code: Select all

ignore_user_abort();

$fp = fopen("log2.txt", "a");
require("config.php");
if ($_GET['key'] != $key) {
	fwrite($fp, "Cheating detected!\n\n\n");
	die("Cheating detected!");
}

$pid = intval($_GET['p']);
$name = $_GET['name'];
$email =$_GET['email'];
$egold = $_GET['egold'];

if ($pid == 1) {
	$dbhost2 = "";  // Your MySQL database host (no / at the end)
	$dbname2 = "";  // Your database name to use for your cycler
	$dbusername2 = "";  // Your database username
	$dbpassword2 = "";  // Your database password
	$connect = mysql_connect($dbhost2, $dbusername2, $dbpassword2);
	mysql_select_db($dbname2);
	$url2 = "$url/update.php?p=1";
}
elseif ($pid == 2) {
	$dbhost2 = "";  // Your MySQL database host (no / at the end)
	$dbname2 = "";  // Your database name to use for your cycler
	$dbusername2 = "";  // Your database username
	$dbpassword2 = "";  // Your database password
	$connect = mysql_connect($dbhost2, $dbusername2, $dbpassword2);
	mysql_select_db($dbname2);
	$url2 = "$url/update.php?p=2";
}
elseif ($pid == 3) {
	$dbhost2 = "";  // Your MySQL database host (no / at the end)
	$dbname2 = "";  // Your database name to use for your cycler
	$dbusername2 = "";  // Your database username
	$dbpassword2 = "";  // Your database password
	$connect = mysql_connect($dbhost2, $dbusername2, $dbpassword2);
	mysql_select_db($dbname2);
	$url2 = "$url/update.php?p=3";
}

$sql = "SELECT id FROM `user` WHERE stage=1 ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
$re = mysql_fetch_array($result);
$ree = $re['id'] + 1;
$sql = "INSERT INTO `user` (id,name,email,egold,stage) VALUES ('$ree','$name','$email','$egold',1)";
$result = mysql_query($sql);
$sql = "UPDATE statistics SET total = total + 1 WHERE id=1";
$result = mysql_query($sql);
mysql_close($connect);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$a = curl_exec($ch);
if (!$a) {
	curl_exec($ch);
}
curl_close($ch);
This should add a new user to one of the databases depending on $pid value (1, 2 or 3), but it doesn't. I don't know, maybe this page isn't executed through curl in the first page, because when I disable $key check and go to insertnewuser.php?name=dfsdf... directly in my browser, the user with the data is added.

Thanks for any help.
Last edited by arukomp on Sat Mar 10, 2007 4:07 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you put some error checking into your code and post back what errors are being thrown?
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

ahhh.... I just needed to add "global $p;" in one of my functions. Because of that the function otherprogram() didn't execute and nothing happened...

I have to say this is kind of stupid mistake :) I personally hate that I need to set globals to be able to use variables in my functions...

Any way, thanks Everah for pointing me to look for errors. I've added some code to log every move (what has been executed and what hasn't been). I've found where it stopped and had a better look at that place and then I found the problem...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Pass the data into the function.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

But it's about 10 variables... And there are about 10 places where script calls that function, so there would be more code with that way
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That may signal an opportunity for refactoring. ;)

It's better to pass in information then for the function to require a certain global to exist to function correctly.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

ok, thanks for your help :)
Post Reply