Staredit Network

Staredit Network -> Computers and Technical -> PHP help needed (new problem #2).
Report, edit, etc...Posted by Centreri on 2006-06-04 at 13:36:15
I'm making a small 'script' in php, and am having some problems in which I have no idea what the problem is.

I'm working on the install part first, in which you put the data in forms, etc etc etc, and on the next page it processes everything, makes sure everything is filled in. If everything isn't filled in, it makes a variable called '$installErrorMessage' and assigns an error message to it. It also makes a boolean, '$installErrorBol' true if there is a problem. It is all the made into $_SESSION[' '] form and you're redirected to the initial installation page, where if the $_SESSION[' '] is set it displays it in a nice red box.

This is the processing page:
CODE
<?php
// Initiate session.
session_start();

// Redirect back to installation index.
header("location: Index.php");

// Make sure variables are usable.
if ( (!isset($_POST['dbUser'])) || (!isset($_POST['dbPass'])) || (!isset($_POST['dbHost'])) || (!isset($_POST['dataBase'])) ||            (!isset($_POST['tablePrefix'])) || (!isset($_POST['adminUser'])) || (!isset($_POST['adminPass'])) || (!isset($_POST['adminPassCheck'])) || (!isset($_POST['adminMail'])) || (!isset($_POST['adminMailCheck'])) )
{
$installErrorMessage = "One or more fields were not filled in. Please make sure all fields are accurately filled in";
$installErrorBol = true;
}


// If there is an error, send error message to index page.
if (isset($installErrorBol) && ($installErrorBol = true))  {
$_SESSION['installErrorMessage'] = $installErrorMessage;
}

?>


Here is the main part of Index.php where you input data:
CODE

<?php session_start(); ?>
<!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=iso-8859-1" />
<title>CentSB - Installation</title>
<link rel="stylesheet" type="text/css" href="../Common/Common.css" />
</head>
<body style="background-color: #CCCCCC;">
<center>
<div style="border-width: thin; border-color: #00CCFF; border-style: solid; width: 500px;"
<div style="background-color: #33CCCC; width: 500px; height: 35px;"> Welcome to CentSB Installation. Please fill out the following forms to continue with installation.<br /></div>
<div style="background-color: red;"><?php if (isset($_SESSION['installErrorMessage'])) { echo $_SESSION['installErrorMessage']; } ?></div>


From experimenting, I found that in the install_Go.php (processing page) the problem is that it doesn't detect that the elements are missing.
Oh, and because the <input> forms have an initial value (value="etc"), if the person doesn't edit that, it counts as if they did, right? Or do browsers not count that?

Oh, and the initial problem - It doesn't show the error message when it redirects. And not everything was filled.
Report, edit, etc...Posted by O)FaRTy1billion on 2006-06-04 at 13:52:14
Try checking if the value is null?

EDIT:
Now that I think about it and thick back to a shoutbox I was making..
CODE
if($username == "") {
$result = "<b>Please enter a name.</b>";
} elseif ($message == "") {
$result = "<b>Please enter a message.</b>";
} else {
(etc..)
Report, edit, etc...Posted by Centreri on 2006-06-04 at 13:58:13
How would that help? Even if they are null, they would still exist and would still have to be detected by isset();, wouldn't they? Or not detected.

Wait a second, if the information is sent through a form, are empty fields sent as a null variables or not sent?
Report, edit, etc...Posted by O)FaRTy1billion on 2006-06-04 at 14:19:23
From what I have seen and done, they are sent as null. ""
Report, edit, etc...Posted by Centreri on 2006-06-04 at 14:30:23
Ah. There's the ticket :/. Thanks.

Edit: Opened for further discussion/suggestions.

New problem: I'm having a part of the processing script connect to the MySQL and if anything goes wrong, from connecting to selecting a database, it is added into the error message which is then sent back to you.
The problem is that when it doesn't work, PHP creates an error message. Is there any way for it to 'accept' that there is an error without printing it out? It isn't seen because you are instantly redirected and doesn't really hinder anything, but is there anyway to remove it? I want a bug-free script even if it doesn't hinder anything -.-. Oh, and even though it doesn't matter, for some reason it goes into this part of the code even if the $_POST values ARE null. Can't figure out why.

Here's the code:

CODE
// Make sure connection to MySQL is possible and works.
if ( (!is_null($_POST['dbUser'])) && (!is_null($_POST['dbPass'])) && (!is_null($_POST['dbHost'])) && (!is_null($_POST['dataBase'])) ) {
// Connect to MySQL.
$mysqlconncheck = mysql_connect($_POST['dbHost'], $_POST['dbUser'], $_POST['dbPass']);
 // If successfully connected, select database.
 if ($mysqlconncheck){
  $mysqlDBcheck = mysql_select_db($_POST['dataBase']);
 }
 // If something went wrong, create error message and make $installErrorBol true.
 if (!$mysqlconncheck) {
  $installErrorMessage = $installErrorMessage . "Unable to connect to MySQL. Make sure all MySQL-related fields are filled accurately. ";
  $installErrorBol = true;
 } elseif (($mysqlconncheck) && (!$mysqlDBcheck)) {
  // If something went wrong, create error message and make $installErrorBol true.
  $installErrorMessage = $installErrorMessage . "Was able to connect to MySQL but unable to select database. Make sure it's accurate. ";
  $installErrorBol = true;
 }
}
Report, edit, etc...Posted by Mr_Mooo_Cow(U) on 2006-06-05 at 18:35:29
putting a @ before a function like mysql_xxxx will ignore errors wink.gif ex.
@mysql_result
Report, edit, etc...Posted by Centreri on 2006-06-05 at 18:57:40
Thanks, worked. I'll keep increasing the number of the 'new problem' for each new problem, and I'll probably have a lot these days.
Report, edit, etc...Posted by RexyRex on 2006-06-06 at 18:43:22
You could use a custom error reporting function. Those are fun. Check out set_error_handler(). smile.gif
Report, edit, etc...Posted by IanMM on 2006-06-07 at 11:28:18
I don't know if you figured this out yet but the rederect should be in an else after the errors. and there's and easier way to check if nothing is filled out.
This is like how I coded my shoutbox:
CODE
<?php
$name=$_POST['name'];
$msg=$_POST['message'];
$time= date("r T");
$color=$_POST['color'];
$time2= date("Ymd");


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


includ
if($name == '' || $name == 'Name' || $msg == '') {
echo "Please fill in your name and/or message";
} else {

header("location: Index.php");

}

?>
Report, edit, etc...Posted by Centreri on 2006-06-07 at 17:03:49
No, I already finished with that. A few things still being worked on in that category, but no problem with that.
Report, edit, etc...Posted by IanMM on 2006-06-07 at 19:50:42
What are you having problems with right now? Then we could help easier.
Report, edit, etc...Posted by Centreri on 2006-06-19 at 17:09:05
Nothing. I'm just keeping this topic for for when I do :/.

New Problem: Cookies.
I want to make a basic login script for my site, which needs cookies (Well...).
I know how to set a cookie, but how do I detect if a user's browser has the stored cookie and read the data from it?
Oh, and 'localhost' is the domain name if you do cookies on your o
Report, edit, etc...Posted by Doodle77(MM) on 2006-06-19 at 17:32:53
$_COOKIE
Report, edit, etc...Posted by Centreri on 2006-06-20 at 16:42:10
Yes, I know that's how a cookie is named.. $_COOKIE['cookiename']. Not very helpful.
Report, edit, etc...Posted by Shmeeps on 2006-06-20 at 18:08:03
CODE

if($_COOKIE['value'] != null)
{
readcookie();
}
else
{
showloginscript();
}

Of course, put you own code in there, as those are pseudofunctions.
Report, edit, etc...Posted by Centreri on 2006-06-21 at 17:20:48
And how do you read the cookie information? Is the $_COOKIE['something'] thing an array?
Report, edit, etc...Posted by Shmeeps on 2006-06-21 at 17:28:33
Yeah, just $_COOKIE['value'].
Report, edit, etc...Posted by Centreri on 2006-06-22 at 15:01:49
So a person can only have 1 cookie at a time, and the $_COOKIE['value'] is one element of the cookie?
Report, edit, etc...Posted by Shmeeps on 2006-06-22 at 15:19:20
Correct, it's just like an array. Sessions work the same way, if you use them like that.

Also, any value set with setcookie() will be in there two, its name will replace value in the $_COOKIE variable.
Report, edit, etc...Posted by Centreri on 2006-06-22 at 17:58:54
Ok, here's basic code. I've tried it and it didn't work. On a seperate file, I have -
CODE

<?php
setcookie("login", "true", time()+3600, "/", "localhost", 0);
header("location: Mainpage.php");
?>

Then, in Mainpage.php,
CODE

<?php
if ($_COOKIE['login'] != "") {
echo "cookie detected: " . $_COOKIE['login'] . "<br />";
}
?>

. It didn't work. I tried replacing $_COOKIE['value'] instead of $_COOKIE['login'] for experimental reasons, didn't work.
Report, edit, etc...Posted by Shmeeps on 2006-06-22 at 21:22:34
For test reasons, take out the if statement. See if it comes up with true or not.

You could also try != null instead of != "".
Report, edit, etc...Posted by Centreri on 2006-06-23 at 16:04:50
Nope, still not working. Are you sure it's supposed to check if the cookie is null, not whether it exists at all? Anyway, as expected, the error notice returned was
Notice: Undefined index: value in C:\Website\www.Centreri.net\Content\Mainpage.php on line 33.
Same when I put in the name of the cookie instead of value (but instead of it saying value, it says the name of the cookie.).
Report, edit, etc...Posted by Shmeeps on 2006-06-23 at 23:46:36
Ok, I tested it on my server, try this:

Replace
CODE

setcookie("login", "true", time()+3600, "/", "localhost", 0);


with
CODE

setcookie("login", "true", time()+3600);


See if that works.
Report, edit, etc...Posted by Centreri on 2006-06-27 at 21:05:40
Worked, thanks. Now cookies are open.
Report, edit, etc...Posted by Shmeeps on 2006-06-27 at 22:28:22
Yeah, alot of those times setting those values can mess up things because of conflicting values in Apache, among other things. I always just leave them empty.
Next Page (1)