Page 1 of 1

What is wrong with this code?...

Posted: Sat May 22, 2010 3:33 am
by Software_Pyrate
Hi guys, I'm fairly new to php..And I can not figure out what is wrong with this code-but I'm sure you guys can so here goes.

I have a user log in using SESSION variables. All I want to do is add a $_COOKIE (If user checks box in form) using the values of my SESSION..

Code: Select all

<?php
 session_start();

if(isset($_POST['radio_button']))
{
//This outputs

		$u = rand();
		setcookie("cook",$u,time() +3600);

//This DOESN'T output...and I can't figure out why 
	
		$user = $_SESSION['MM_Username'];
		setcookie("user",$user, time() +3600);
}
?>
<html>
If I pass the the SESSION into a variable and echo it out, it works-just fine
$a = $_SESSION['test'];
echo $a;

But when I try to use it in setcookie() it doesn't, i.e. example above. I've also tried $_POST to no avail.:banghead:
$user = $_POST['MM_Username'];
setcookie("user",$user, time() +3600);


Here is all of it

Code: Select all

<?php require_once('Connections/login.php'); ?>

<?php
 session_start();

if(isset($_POST['radio_button']))
				{
		$u = rand();
					setcookie("cook",$u,time() +3600);
	
		$user = $_SESSION['MM_Username'];
					setcookie("user",$user, time() +3600);
				}
?>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}




?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['name'])) {
  $loginUsername=$_POST['name'];
  $password=$_POST['pass'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "kepp_login_with_cookie.php";
  $MM_redirectLoginFailed = "kepp_login_with_cookie.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_login, $login);
  
  $LoginRS__query=sprintf("SELECT user_name, user_pass FROM login WHERE user_name=%s AND user_pass=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $login) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body><form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<table width="400" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>name</td>
    <td>
      <label>
        <input type="text" name="name" id="name" />
      </label>
   </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>pass</td>
    <td>
      <label>
        <input type="password" name="pass" id="pass" />
      </label>
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
      <label>
        <input type="submit" name="submit" id="submit" value="Submit" />
      </label>
      <label>
        <input type="radio" name="check_box" id="stay" value="stay" />
      </label></td>
    <td>&nbsp;</td>
  </tr>
</table>  </form>

<p>You are logged in as
<?php
if(!isset($_SESSION['MM_Username']))

{
	echo "guest";
}
else
{
	echo $_SESSION['MM_Username'];
	 
}

?>

<br/>
</p>
<p>&nbsp;</p>
<p><a href="clear_session.php">sign out</a></p>







</body>
</html>
How can I get my SESSION['MM_Username'] value into my Cookie value

Re: What is wrong with this code?...

Posted: Sat May 22, 2010 5:34 pm
by lcarron000
Does this come from another form?

Code: Select all

$_POST['radio_button'] 
If this is from the form you have posted the code won't be executed. the if condition will return false because $_POST['radio_button] does
not exist. it should be

Code: Select all

if (isset($_POST['stay'])) {
    $u = rand();
    setcookie("cook", $u, time() + 3600);

    $user = $_SESSION['MM_Username'];
    setcookie("user", $user, time() + 3600);
}

Re: What is wrong with this code?...

Posted: Sun May 23, 2010 8:28 pm
by Software_Pyrate
Hey, thanks for the reply...

You are absolutely right :wink: 8)

I cannot believe I made such a rookie mistake :banghead: :crazy:


:D ...U the man


**edit: I have edited the code to work correctly

Thanks

Mods, please delete this embarrassment. :oops:

Re: What is wrong with this code?...

Posted: Sun May 23, 2010 9:01 pm
by requinix
Software_Pyrate wrote:Mods, please delete this embarrassment. :oops:
What? So we can all forget about it? Not a chance :twisted:

Re: What is wrong with this code?...

Posted: Mon May 24, 2010 6:49 am
by lcarron000
No problem. Glad I could help.
We overlook the simple mistakes because we don't expect to make them.