Jump to content
xisto Community
Sign in to follow this  
iGuest

$_get Issue (urgent!) Get variables not working

Recommended Posts

I have this really urgent problem with $_GET variables. They aren't working properly, and my site's pretty much based around them. Take this example from my Personal Messenger system.index.php?p=messenger&action=composeThis should take you to the compose message page of the messenger, but it doesn't. It recognises the '?p=messenger' bit, and takes you to the messenger page, but it completely ignores the '&action=compose' bit. For some reason, it occurs in some parts of the site, and not in others. Could you please fix this problem ASAP? It's a really urgent issue, and my site wont work otherwise!!! I'm 99% sure it's not a problem with my site, as it worked fine when I was doing it on my other server. I have no idea why it's only doing it on some pages though!

Share this post


Link to post
Share on other sites

I would with that 1% that the problem is within your web site. GET and POST requests are one of the most important aspects of PHP and I can't imagine them not working properly. What's more, my Joomla! web site (which uses multiple GET requests on each page) works without a single glitch. That's why you should make sure you are properly using $_GET['messenger'] and $_GET['action'] :P

Share this post


Link to post
Share on other sites

My guess is that your previous server used PHP4 and our server uses PHP5. If you are using older PHP3 functions in your scripts that are no longer supported, then this would cause the problem.

 

I can't imagine any other issue which would cause this problem. As mentioned by pyost, most PHP scripts utilize these superglobal variables and nobody else is having said issues. It is important to remember that the error could exist somewhere other than the retrieval of the superglobal variable value. For example, if the function for compose has an error in it because of a database error, PHP5 incompatibility issue, or even a file permission setting, the script may be written to return to the default page of the master script which in this case would be index.php?p=messenger.

 

My suggestion is to review your error logs or better yet, upgrade your PM system.

 

Please keep us up to date on your status with this problem.

 

vujsa

Share this post


Link to post
Share on other sites

Well, my other server is my own, and that uses PHP4, so that might be the problem, though I doubt it. I use an if statement on the messenger page to check for the action, it's structured like this:

if ($_GET["action"]=="compose"){	//Actions here}else if ($_GET["action"]=="sendpm"){	//Actions here}etc. etc.

I don't know if calling $_GET variables in PHP5 is any different to in PHP4, I don't think so, but the difference on the pages that do work, I've noticed is that there are if statements in the various parts of the page where I want things to happen depending on the $_GET["action"], whereas here there's just one big if ... else statement. Could that have anything to do with it?

Share this post


Link to post
Share on other sites

u could look at the php5 documentation to see what the correct structure is, and to see if its different or the same as php4.

Share this post


Link to post
Share on other sites

The piece of code you posted looks perfectly fine, and should work withour problems, but I would advise you to modify it a bit in order to achieve better coding. Instead of using $_GET["action"] all the time, enter the following code at the begining of the script:

 

$action = '';if ( isset($_GET['action']) ) {   $action = $_GET['action'];}
This way you avoid checking a variable which is not set - in that case, you would just have an empty string in $action. Furthermore, you don't have to use if..elseif..elseif..etc all the time, because the switch structure is exactly what you need. So, what you posted would look like this:

 

switch ($action) {   case 'compose':		 // Actions here		 break;   case 'sendpm':		 // Actions here		 break;}

Back to the problematic topic; you might want to add error_reporting(E_ALL) to the begining of all your scripts while developing them, in order to see both "malicious" and "non-malicious" errors PHP might produce while executing them. And when you are sure everything works fine, replace it with error_reporting(0) so as to avoiding users being able to see errors (if they occur).

Share this post


Link to post
Share on other sites

Ok, here it is (The filename is messenger.php)

<?phperror_reporting(E_ALL);if ($authorisation==false){  echo '<script language="JavaScript" type="Text/JavaScript">document.location = \'?p=home\';</script>';}?><center><img src="images/banner_usercp.gif"></center><hr><?php include("usernav.php"); ?><hr><br><br><br><table width="100%"><tr><td width="25%" valign="top"><b>Habble Personal Messenger</b><br><br><a href="?p=messenger">Inbox (<?php echo $new; ?>)</a><br><a href="?p=messenger&action=compose">Compose Message</a><br><a href="?p=messenger&action=sentmail">Sent Mail</a><br><a href="?p=messenger&action=drafts">Drafts</a><br><a href="?p=messenger&action=trash">Trash</a><br></td><td width="75%" valign="top"><?phpif ($_GET["action"]=="compose"){  if ($_POST["to"])  {	$to = $_POST["to"];  }  else  {	$to = "";  }  if ($_POST["subject"])  {	$subject = $_POST["subject"];  }  else  {	$subject = "";  }  if ($_POST["message"])  {	$message = $_POST["message"];	$message = str_replace("\\\\", "\\", $message);	$message = str_replace("\'", "'", $message);	$message = str_replace('\"', '"', $message);	$message = str_replace("<br>", "\r\n", $message);	$message = str_replace("%--sq--%", '\'', $message);	$message = str_replace("%--dq--%", "\"", $message);  }  else  {	$message = "";  }  echo '<span style="font-size: 15px; font-weight: bold;">Compose a Personal Message</span><br><br>';  if ($_GET["reason"]=="blankfields")  {	echo '<span style="color: #FF0000;">You cannot leave the recipients or message fields blank!</span><br>';  }  echo '<form action="?p=messenger&action=sendpm" method="POST" name="pmform"><b>Recipients:</b> (One per line)<br><textarea name="recipients" rows="3" id="recipients" class="smalltextnorm" onFocus="smalltextfocus(\'recipients\');" onBlur="smalltextblur(\'recipients\');">'.$to.'</textarea><br><br>';  echo '<b>Subject:</b><br><input type="text" name="subject" id="subject" class="smalltextnorm" onFocus="smalltextfocus(\'subject\');" onBlur="smalltextblur(\'subject\');" value="'.$subject.'"><br><br>';  echo '<b>Smileys:</b><br>';  include ("smileys.php");  echo '<br><br>';  echo '<b>Message:</b><br><textarea name="message" rows="10" id="message" class="textnorm" onFocus="textfocus(\'message\');" onBlur="textblur(\'message\');">'.$message.'</textarea><br><br>';  echo '<input type="submit" value="Send" class="button"><input type="button" value="Save" class="button" onClick="saveDraft();"></form>';}else if ($_GET["action"]=="sendpm"){  if (!$_POST["recipients"] or !$_POST["message"])  {	$to = $_POST["recipients"];	$bcc = $_POST["bcc"];	$message = $_POST["message"];	echo '<b>You left fields blank, now taking you back, please wait...</b>';	echo '<form name="blankfields" action="?p=messenger&action=compose&reason=blankfields" method="POST"><input type="hidden" name="to" value="'.$to.'"><input type="hidden" name="bcc" value="'.$bcc.'"><input type="hidden" name="message" value="'.$message.'"></form>';	echo '<script>document.blankfields.submit();</script>';  }  else  {	include("dbconnect.php");	$recipients = $_POST["recipients"];	$recipients = str_replace("\r\n", "%", $recipients);	$recipients = str_replace("\r", "%", $recipients);	$recipients = str_replace("\n", "%", $recipients);	$bcc = $_POST["bcc"];	$bcc = str_replace("\r\n", "%", $bcc);	$bcc = str_replace("\r", "%", $bcc);	$bcc = str_replace("\n", "%", $bcc);	if ($_POST["subject"])	{	  $subject = $_POST["subject"];	}	else	{	  $subject = "(No Subject)";	}	$message = $_POST["message"];	$message = str_replace("\r\n", "<br>", $message);	$message = str_replace("\r", "<br>", $message);	$message = str_replace("\n", "<br>", $message);	$message = str_replace(":)", '<img src="images/smileys/happy.gif">', $message);	$message = str_replace(">:(", '<img src="images/smileys/angry.gif">', $message);	$message = str_replace(":(", '<img src="images/smileys/sad.gif">', $message);	$message = str_replace(":S", '<img src="images/smileys/confused.gif">', $message);	$message = str_replace(":o", '<img src="images/smileys/surprised.gif">', $message);	$message = str_replace(":D", '<img src="images/smileys/laughing.gif">', $message);	$message = str_replace("B)", '<img src="images/smileys/cool.gif">', $message);	$toarray = split("%", $recipients);	$tomax = count($toarray);	$topos = 0;	while($topos!=$tomax)	{	  $sendpm = "insert into messages values('".$_COOKIE["habble_username"]."', '".$toarray[$topos]."', '".$subject."', '".$message."', 'inbox', '".time()."', '0', NULL);";	  mysql_query($sendpm) or die('<span style="color: #FF0000; font-weight: bold;">Error! Your PM to '.$toarray[$topos].' was not sent. Please send us an email about this problem <a href="?p=contact">here</a>. Make sure you quote the following message in your email:<br><br>'.mysql_error().'</span><br>');	  echo '<span style="color: #0000FF; font-weight: bold;">PM Successfully sent to '.$toarray[$topos].'!</span><br>';	  $topos = $topos + 1;	}  }}else if ($_GET["action"]=="sentmail"){  echo '<table width="100%" cellspacing="0">';  $getpm = "select * from messages where sender = '".$_COOKIE["habble_username"]."' order by mid desc;";  $gpmresult = mysql_query($getpm);  $altbg = 0;  while($gpmarray=mysql_fetch_array($gpmresult))  {	if ($gpmarray["read"]=="0")	{	  $img = "unreadmail.gif";	}	else if ($gpmarray["read"]=="1")	{	  $img = "readmail.gif";	}	if ($altbg==0)	{	  echo '<tr><td class="message1"><table><tr><td><img src="images/'.$img.'"></td><td><span style="font-size: 15px;"><a href="?p=messenger&action=readsent&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>To '.$gpmarray["recipient"].'</td></tr></table></td></tr>';	  $altbg = 1;	}	else if ($altbg==1)	{	  echo '<tr><td class="message2"><table><tr><td><img src="images/'.$img.'"></td><td><span style="font-size: 15px;"><a href="?p=messenger&action=readsent&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>To '.$gpmarray["recipient"].'</td></tr></table></td></tr>';	  $altbg = 0;	}  }  echo '</table>';}else if ($_GET["action"]=="drafts"){  echo '<table width="100%" cellspacing="0">';  $getpm = "select * from messages where sender = '".$_COOKIE["habble_username"]."' and folder = 'drafts';";  $gpmresult = mysql_query($getpm);  $altbg = 0;  while($gpmarray=mysql_fetch_array($gpmresult))  {	if ($gpmarray["read"]=="0")	{	  $img = "unreadmail.gif";	}	else if ($gpmarray["read"]=="1")	{	  $img = "readmail.gif";	}	$to = str_replace("\n", "<br>", $gpmarray["recipient"]);	$to = str_replace("\r\n", "<br>", $to);	$to = str_replace("\r", "<br>", $to);	$message = str_replace("\n", "<br>", $gpmarray["message"]);	$message = str_replace("\r\n", "<br>", $message);	$message = str_replace("\r", "<br>", $message);	$message = str_replace("\\\\", "\\", $message);	$message = str_replace("\'", "'", $message);	$message = str_replace('\"', '"', $message);	$message = str_replace('"', '%--dq--%', $message);	$message = str_replace("'", "%--sq--%", $message);	if ($altbg==0)	{	  echo '<tr><td class="message1"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><form action="?p=messenger&action=compose" method="POST" id="draft'.$gpmarray["mid"].'"><input type="hidden" name="to" value="'.$to.'"><input type="hidden" name="subject" value="'.$gpmarray["subject"].'"><input type="hidden" name="message" value="'.$message.'"></form><span style="font-size: 15px;"><a href="#" onClick="document.getElementById(\'draft'.$gpmarray["mid"].'\').submit()">'.$gpmarray["subject"].'</a> |</span>To '.$gpmarray["recipient"].'</td><td class="seethrough"><a href="?p=messenger&action=trashdraft&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 1;	}	else if ($altbg==1)	{	  echo '<tr><td class="message2"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><form action="?p=messenger&action=compose" method="POST" id="draft'.$gpmarray["mid"].'"><input type="hidden" name="to" value="'.$to.'"><input type="hidden" name="subject" value="'.$gpmarray["subject"].'"><input type="hidden" name="message" value="'.$message.'"></form><span style="font-size: 15px;"><a href="#" onClick="document.getElementById(\'draft'.$gpmarray["mid"].'\').submit()">'.$gpmarray["subject"].'</a> |</span>To '.$gpmarray["recipient"].'</td><td class="seethrough"><a href="?p=messenger&action=trashdraft&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 0;	}  }  echo '</table>';}else if ($_GET["action"]=="savedraft"){  include("dbconnect.php");  if ($_POST["subject"])  {	$subject = $_POST["subject"];  }  else  {	$subject = "(No Subject)";  }  $message = $_POST["message"];  $sendpm = "insert into messages values('".$_COOKIE["habble_username"]."', '".$_COOKIE["habble_username"]."', '".$subject."', '".$message."', 'drafts', '".time()."', '0', NULL);";  mysql_query($sendpm) or die('<span style="color: #FF0000; font-weight: bold;">Error! Your draft was not saved. Please send us an email about this problem <a href="?p=contact">here</a>. Make sure you quote the following message in your email:<br><br>'.mysql_error().'</span><br>');  echo '<span style="color: #0000FF; font-weight: bold;">Draft successfully saved!</span><br>';}else if ($_GET["action"]=="readpm"){  include("dbconnect.php");  $getmsgdata = "select * from messages where mid = '".$_GET["message"]."' and recipient = '".$_COOKIE["habble_username"]."';";  $gmdresult = mysql_query($getmsgdata);  $markasread = "update `messages` set `read` = '1' where `mid` = '".$_GET["message"]."';";  mysql_query($markasread) or die('Could not mark message as read. This problem should be reported. Please report it <a href="?p=contact">here</a>. Make sure you quote the following message:<br><br>'.mysql_error());  $gmdarray = mysql_fetch_array($gmdresult);  $date = "on ".date("jS F Y", $gmdarray["date"]);  if ($date=="on ".date("jS F Y", time()))  {	$date = "today";  }  $time = date("g:i:s a", $gmdarray["date"]);  $message = str_replace("\\\\", "\\", $gmdarray["message"]);  $message = str_replace("\'", "'", $message);  $message = str_replace('\"', '"', $message);  $olmessage = str_replace('"', '%--dq--%', $gmdarray["message"]);  $olmessage = str_replace("'", "%--sq--%", $olmessage);  echo '<span style="font-size: 15px; font-weight: bold;">'.$gmdarray["subject"].'  From '.$gmdarray["recipient"].'</span><br><table><tr><td><form action="?p=messenger&action=compose" method="POST"><input type="hidden" name="to" value="'.$gmdarray["sender"].'"><input type="hidden" name="subject" value="Re: '.$gmdarray["subject"].'"><input type="hidden" name="message" value="'.'<br><br><br>--------------------------------<br>Original message sent '.$date.' at '.$time.' by '.$gmdarray["sender"].'<br>--------------------------------<br>'.$olmessage.'"><input type="submit" class="button" value="Reply"></form></td><td><form action="?p=messenger&action=compose" method="POST"><input type="hidden" name="subject" value="Fw: '.$gmdarray["subject"].'"><input type="hidden" name="message" value="'.'<br><br><br>--------------------------------<br>Original message sent '.$date.' at '.$time.' by '.$gmdarray["sender"].'<br>--------------------------------<br>'.$olmessage.'"><input type="submit" class="button" value="Forward"></form></td><td><form action="?p=messenger&action=trashmessage&message='.$gmdarray["mid"].'" method="POST"><input type="submit" class="button" value="Send to trash"></form></td></tr></table><hr><br>'.$message.'<hr><span style="font-size: 9px;">Sent '.$date.' at '.$time.'</span>';}else if ($_GET["action"]=="readsent"){  include("dbconnect.php");  $getmsgdata = "select * from messages where mid = '".$_GET["message"]."' and sender = '".$_COOKIE["habble_username"]."';";  $gmdresult = mysql_query($getmsgdata);  $gmdarray = mysql_fetch_array($gmdresult);  $date = "on ".date("jS F Y", $gmdarray["date"]);  if ($date=="on ".date("jS F Y", time()))  {	$date = "today";  }  $time = date("g:i:s a", $gmdarray["date"]);  $message = str_replace("\\\\", "\\", $gmdarray["message"]);  $message = str_replace("\'", "'", $message);  $message = str_replace('\"', '"', $message);  $olmessage = str_replace('"', '%--dq--%', $gmdarray["message"]);  $olmessage = str_replace("'", "%--sq--%", $olmessage);  echo '<span style="font-size: 15px; font-weight: bold;">'.$gmdarray["subject"].'  From '.$gmdarray["recipient"].'</span><br><table><tr><td><form action="?p=messenger&action=compose" method="POST"><input type="hidden" name="subject" value="Fw: '.$gmdarray["subject"].'"><input type="hidden" name="message" value="'.'<br><br><br>--------------------------------<br>Original message sent '.$date.' at '.$time.' by '.$gmdarray["sender"].'<br>--------------------------------<br>'.$olmessage.'"><input type="submit" class="button" value="Forward"></form></td></tr></table><hr><br>'.$message.'<hr><span style="font-size: 9px;">Sent '.$date.' at '.$time.'</span>';}else if ($_GET["action"]=="trashmessage"){  include("dbconnect.php");  $sendtotrash = "update messages set folder = 'trash' where mid = '".$_GET["message"]."' and recipient = '".$_COOKIE["habble_username"]."';";  mysql_query($sendtotrash) or die('<span style="color: #FF0000; font-weight: bold;">The message could not be sent to the trash. Please send us an email about this problem <a href="?p=contact">here</a>, and we will try to fix it as soon as possible. Please quote the following message in your email:<br><br>'.mysql_error().'</span>');  echo '<span style="color: #0000FF; font-weight: bold;">The message was successfully sent to the trash</span>';}else if ($_GET["action"]=="trashdraft"){  include("dbconnect.php");  $sendtotrash = "update messages set folder = 'trash', recipient = '".$_COOKIE["habble_username"]."' where mid = '".$_GET["message"]."' and sender = '".$_COOKIE["habble_username"]."';";  mysql_query($sendtotrash) or die('<span style="color: #FF0000; font-weight: bold;">The message could not be sent to the trash. Please send us an email about this problem <a href="?p=contact">here</a>, and we will try to fix it as soon as possible. Please quote the following message in your email:<br><br>'.mysql_error().'</span>');  echo '<span style="color: #0000FF; font-weight: bold;">The message was successfully sent to the trash</span>';}else if ($_GET["action"]=="trash"){  echo '<table width="100%" cellspacing="0">';  $getpm = "select * from messages where recipient = '".$_COOKIE["habble_username"]."' and folder = 'trash' order by mid desc;";  $gpmresult = mysql_query($getpm);  $altbg = 0;  while($gpmarray=mysql_fetch_array($gpmresult))  {	if ($gpmarray["read"]=="0")	{	  $img = "unreadmail.gif";	}	else if ($gpmarray["read"]=="1")	{	  $img = "readmail.gif";	}	if ($altbg==0)	{	  echo '<tr><td class="message1"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><span style="font-size: 15px;"><a href="?p=messenger&action=readpm&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>From '.$gpmarray["sender"].'</td><td class="seethrough"><a href="?p=messenger&action=delete&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 1;	}	else if ($altbg==1)	{	  echo '<tr><td class="message2"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><span style="font-size: 15px;"><a href="?p=messenger&action=readpm&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>From '.$gpmarray["sender"].'</td><td class="seethrough"><a href="?p=messenger&action=delete&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 0;	}  }  echo '</table>';}else if ($_GET["action"]=="delete"){  include("dbconnect.php");  $delete = "delete from messages where mid = '".$_GET["message"]."' and recipient = '".$_COOKIE["habble_username"]."';";  mysql_query($delete) or die('<span style="color: #FF0000; font-weight: bold;">The message could not be deleted. Please send us an email <a href="?p=contact">here</a> about this problem. make sure you quote the following message in your email:<br><br>'.mysql_error().'</span>');  echo '<span style="color: #0000FF; font-weight: bold;">Message deleted.</span>';}else{  echo '<table width="100%" cellspacing="0">';  $getpm = "select * from messages where recipient = '".$_COOKIE["habble_username"]."' and folder = 'inbox' order by mid desc;";  $gpmresult = mysql_query($getpm);  $altbg = 0;  while($gpmarray=mysql_fetch_array($gpmresult))  {	if ($gpmarray["read"]=="0")	{	  $img = "unreadmail.gif";	}	else if ($gpmarray["read"]=="1")	{	  $img = "readmail.gif";	}	if ($altbg==0)	{	  echo '<tr><td class="message1"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><span style="font-size: 15px;"><a href="?p=messenger&action=readpm&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>From '.$gpmarray["sender"].'</td><td class="seethrough"><a href="?p=messenger&action=trashmessage&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 1;	}	else if ($altbg==1)	{	  echo '<tr><td class="message2"><table><tr><td><img src="images/'.$img.'"></td><td width="100%"><span style="font-size: 15px;"><a href="?p=messenger&action=readpm&message='.$gpmarray["mid"].'">'.$gpmarray["subject"].'</a> |</span>From '.$gpmarray["sender"].'</td><td class="seethrough"><a href="?p=messenger&action=trashmessage&message='.$gpmarray["mid"].'"><img src="images/button_delete.gif"></td></tr></table></td></tr>';	  $altbg = 0;	}  }  echo '</table>';}?></td></tr></table><hr>

Share this post


Link to post
Share on other sites

Fixed it. I think the problem must have been that I had a script at the top of the index page checking $_GET["action"] to see if a user logged in, and to make a cookie. For some reason, I think it was interfering with this, so I changed it to $_GET["msgaction"] and it's working fine now.

Share this post


Link to post
Share on other sites

oh yay u got the pm thing fixed!!!u double posted, i always tell u not to but thats like the 5th time uve double posted.btw, y werent u at school 2day?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.