...

View Full Version : Help - PHP form Issue



vilify
01-14-2012, 12:50 PM
My Issue: My form will not submit. It will just refresh login.php, whereas it should be posting to login_sub.php
was handling all the login with login.php, but decided to add that to a different page to further diagnose the problem.

I really dont understand why it wouldnt submit.

Im running these and some other functions on a few other pages right now, like inserting into mysql, and those work perfectly fine.


The code:

Includes on login.php

<?php include('lib/class_lib.php'); ?>
<?php
$session = new funcs();
$session->session();

$user_login = new funcs();
$user_login->login_submit();

$user_logout = new funcs();
$user_logout->logout();
?>

My login.php form


<form name="login" action="login_sub.php" method="POST">
<label>Username:</label>
<input name="uname" type="text" />
<br /><br />
<label>Password:</label>
<input name="pword" type="password" />
<br /><br />
<input name="submit" value="Log In" type="submit" />
</form>


the called functions. login_submit() is irrelivent atm because I have replaced calling that variable with just a straight forward action


function session() {
if (!isset($_SESSION)) {
session_start();
}
}
function logout() {
// ** Logout the current user. **
$this->logout_link = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$this->logout_link .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$this->logout_direct = "login.php";
if ($this->logout_direct) {
header("Location: $this->logout_direct");
exit;
}
}
}

12k
01-14-2012, 06:12 PM
I see no reason you shouldn't atleast be getting directed to the login_sub.php page. Try to make sure for some odd reason it isnt stored into your browsers cache or something odd. Visit the page and before you submit a form, view the html source code using your browser, and make sure the action is set to action="login_sub.php".

_Aerospace_Eng_
01-14-2012, 08:13 PM
Change this

function logout() {
// ** Logout the current user. **
$this->logout_link = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$this->logout_link .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$this->logout_direct = "login.php";
if ($this->logout_direct) {
header("Location: $this->logout_direct");
exit;
}
}
}
to this

function logout() {
// ** Logout the current user. **
$this->logout_link = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$this->logout_link .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
echo 'got here.';
exit();
$this->logout_direct = "login.php";
if ($this->logout_direct) {
header("Location: $this->logout_direct");
exit;
}
}
}
And post your results. If you go the words "got here" then your logout function is redirecting to login.php because it thinks doLogout is set some how.

vilify
01-15-2012, 01:33 AM
Change this
And post your results. If you go the words "got here" then your logout function is redirecting to login.php because it thinks doLogout is set some how.

Changed what you said, even tried commenting out the code that calls the functions. No results. Still refreshes.

Going to rewrite a new simple login.php with no php and see what it does.

vilify
01-15-2012, 01:44 AM
May have found the issue, but I'm tired, and cant figure it out lol.. also a bit new to classes and functions.

getting: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given


function login($post_user, $post_pass) {
if (isset($_POST['uname'])) {
$this->login_user = $post_user;
$this->login_pass = $post_pass;
$this->userAuthorization = "";
$this->direct_success = "index.php";
$this->direct_fail = "";
$this->direct_refer = false;

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs);
$this->found_user = mysql_num_rows($this->login_query);
if ($this->found_user) {
$this->user_group = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = '$this->login_user';
$_SESSION['MM_UserGroup'] = '$this->user_group';

if (isset($_SESSION['PrevUrl']) && false) {
$this->direct_success = $_SESSION['PrevUrl'];
}
header("Location: " . $this->direct_success );
}
else {
header("Location: ". $this->direct_fail );
}
}
}

vilify
01-15-2012, 01:52 AM
Added a few echo codes to see where it was getting.

It is getting the user information, login_rs gets set properly, but getting a blank output from login_query.

_Aerospace_Eng_
01-15-2012, 02:43 AM
So something may be happening with your query. Change this

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs);
to this

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs) or die(mysql_error().'<br>'.$this->login_rs)
Post your results. My guess is your query might be failing.

vilify
01-15-2012, 03:16 AM
So something may be happening with your query. Change this

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs);
to this

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs) or die(mysql_error().'<br>'.$this->login_rs)
Post your results. My guess is your query might be failing.

Good call. No database selected.
correctly echoes login_rs below that.

variable declared in class. var $db = "grow";


function db_connect() {
//Connect to
$this->db_connect or die(mysql_error());
mysql_select_db($this->db);
}

found that i wasnt calling db_connect, so I did that a few different ways, but its still not working. same error.

_Aerospace_Eng_
01-15-2012, 03:59 AM
Can you post your db_connect function removing your sensitive information of course. Posting your entire class might help as well.

vilify
01-15-2012, 04:16 AM
<?php
//DB Class
class funcs {
//Variables
var $db_connect = "mysql_connect('localhost', 'user', 'pass')";
var $db_select = "mysql_select_db('dbname')";
var $dbtable;
var $theValue;
var $theType;
var $theDefinedValue;
var $login_action;
var $login_user;
var $login_pass;
var $userAuthorization;
var $direct_success;
var $direct_fail;
var $direct_refer;
var $login_rs;
var $login_query;
var $found_user;
var $user_group;
var $logout_link;
var $logout_direct;
var $uname;
var $upass;
var $uemail;
var $userid;
var $plant_strain;
var $plant_stage;
var $plant_num;
var $plant_month;
var $plant_day;
var $plant_year;
var $plant_flower;
//Start Session
function session() {
if (!isset($_SESSION)) {
session_start();
}
}
//Connect DB
function db_connect() {
//Connect to
$this->db_connect or die(mysql_error());
$this->db_select;
}

//Insert User
function insert_user($table, $user, $pass, $email) {
//Insert
$this->dbtable = $table;
$this->uname = $user;
$this->upass = $pass;
$this->uemail = $email;
mysql_query("INSERT INTO $this->dbtable (id, username, password, email) VALUES ('NULL', '$this->uname', '$this->upass', '$this->uemail')");
}
//Insert Plant
function insert_plant($table, $uid, $p_strain, $p_stage, $p_num, $p_month, $p_day, $p_year, $p_flower) {
//Insert
$this->dbtable = $table;
$this->userid = $uid;
$this->plant_strain = $p_strain;
$this->plant_stage = $p_stage;
$this->plant_num = $p_num;
$this->plant_month = $p_month;
$this->plant_day = $p_day;
$this->plant_year = $p_year;
$this->plant_flower = $p_flower;
mysql_query("INSERT INTO $this->dbtable (id, uid, strain, stage, p_num, month, day, year, flower) VALUES ('NULL', '$this->userid', '$this->plant_strain', '$this->plant_stage', '$this->plant_num', '$this->plant_month', '$this->plant_day', '$this->plant_year', '$this->plant_flower')");
}
//Login Link
function login_submit() {
$this->login_action = $_SERVER['PHP_SELF'];
}
//Login User
function login($post_user, $post_pass) {
if (isset($_POST['uname'])) {
$this->login_user = $post_user;
$this->login_pass = $post_pass;
$this->userAuthorization = "";
$this->direct_success = "index.php";
$this->direct_fail = "";
$this->direct_refer = false;

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs) or die(mysql_error().'<br>'.$this->login_rs);
$this->found_user = mysql_num_rows($this->login_query);
if ($this->found_user) {
$this->user_group = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = '$this->login_user';
$_SESSION['MM_UserGroup'] = '$this->user_group';

if (isset($_SESSION['PrevUrl']) && false) {
$this->direct_success = $_SESSION['PrevUrl'];
}
header("Location: " . $this->direct_success );
}
else {
header("Location: ". $this->direct_fail );
}
}
}
//Logout User
function logout() {
// ** Logout the current user. **
$this->logout_link = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$this->logout_link .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['uid'] = NULL;
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['uid']);
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$this->logout_direct = "login.php";
if ($this->logout_direct) {
header("Location: $this->logout_direct");
exit;
}
}
}
//New Function
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
if (PHP_VERSION < 6) {
$this->theValue = get_magic_quotes_gpc() ? stripslashes($this->theValue) : $this->theValue;
}

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

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

?>

BluePanther
01-15-2012, 08:51 AM
<?php
//DB Class
class funcs {
//Variables
var $db_connect = "mysql_connect('localhost', 'user', 'pass')";
var $db_select = "mysql_select_db('dbname')";
var $dbtable;
var $theValue;
var $theType;
var $theDefinedValue;
var $login_action;
var $login_user;
var $login_pass;
var $userAuthorization;
var $direct_success;
var $direct_fail;
var $direct_refer;
var $login_rs;
var $login_query;
var $found_user;
var $user_group;
var $logout_link;
var $logout_direct;
var $uname;
var $upass;
var $uemail;
var $userid;
var $plant_strain;
var $plant_stage;
var $plant_num;
var $plant_month;
var $plant_day;
var $plant_year;
var $plant_flower;
//Start Session
function session() {
if (!isset($_SESSION)) {
session_start();
}
}
//Connect DB
function db_connect() {
//Connect to
$this->db_connect or die(mysql_error());
$this->db_select;
}

//Insert User
function insert_user($table, $user, $pass, $email) {
//Insert
$this->dbtable = $table;
$this->uname = $user;
$this->upass = $pass;
$this->uemail = $email;
mysql_query("INSERT INTO $this->dbtable (id, username, password, email) VALUES ('NULL', '$this->uname', '$this->upass', '$this->uemail')");
}
//Insert Plant
function insert_plant($table, $uid, $p_strain, $p_stage, $p_num, $p_month, $p_day, $p_year, $p_flower) {
//Insert
$this->dbtable = $table;
$this->userid = $uid;
$this->plant_strain = $p_strain;
$this->plant_stage = $p_stage;
$this->plant_num = $p_num;
$this->plant_month = $p_month;
$this->plant_day = $p_day;
$this->plant_year = $p_year;
$this->plant_flower = $p_flower;
mysql_query("INSERT INTO $this->dbtable (id, uid, strain, stage, p_num, month, day, year, flower) VALUES ('NULL', '$this->userid', '$this->plant_strain', '$this->plant_stage', '$this->plant_num', '$this->plant_month', '$this->plant_day', '$this->plant_year', '$this->plant_flower')");
}
//Login Link
function login_submit() {
$this->login_action = $_SERVER['PHP_SELF'];
}
//Login User
function login($post_user, $post_pass) {
if (isset($_POST['uname'])) {
$this->login_user = $post_user;
$this->login_pass = $post_pass;
$this->userAuthorization = "";
$this->direct_success = "index.php";
$this->direct_fail = "";
$this->direct_refer = false;

$this->login_rs = "SELECT username, password FROM users WHERE username='$this->login_user' AND password='$this->login_pass'";

$this->login_query = mysql_query($this->login_rs) or die(mysql_error().'<br>'.$this->login_rs);
$this->found_user = mysql_num_rows($this->login_query);
if ($this->found_user) {
$this->user_group = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = '$this->login_user';
$_SESSION['MM_UserGroup'] = '$this->user_group';

if (isset($_SESSION['PrevUrl']) && false) {
$this->direct_success = $_SESSION['PrevUrl'];
}
header("Location: " . $this->direct_success );
}
else {
header("Location: ". $this->direct_fail );
}
}
}
//Logout User
function logout() {
// ** Logout the current user. **
$this->logout_link = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$this->logout_link .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['uid'] = NULL;
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['uid']);
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$this->logout_direct = "login.php";
if ($this->logout_direct) {
header("Location: $this->logout_direct");
exit;
}
}
}
//New Function
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
if (PHP_VERSION < 6) {
$this->theValue = get_magic_quotes_gpc() ? stripslashes($this->theValue) : $this->theValue;
}

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

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

?>

I would separate out your classes. Seems you're trying to combine everything into one class, rather than split the logic. I would have a think about more specific classes (like a user class, a database class) and take it from there. It makes everything so much more modular, and easier to understand. It also helps you for the future - you can carry all the more specific, modular classes over to future projects.

For a database class, I would make use of the constructor magic method like this:


class db {

// Think about variable scope as well. By making a variable private, it can only be accessed by instances of this class.
// If you plan on extending this class with another class, you can change the scope to protected to allow access through the extended instance.
private $connection;

__construct($host,$user,$pass,$database){
$this->connection = mysql_connect($host,$user,$pass);
if($this->connection != FALSE){
$db = mysql_select_db($host,$this->connection);
if(!$db){
// Some more error reporting
}
}
else{
// Do some error reporting - exceptions maybe
}
}

}

// Defining a class like that, means you can do this
// Could use try and catch for error reporting, if exceptions used (recommended)
$db = new db('localhost','user','pass','foo');

However, for your particular class - you have a variable called db_connect, and a method called db_connect. I'm not sure now PHP would react to that (obviously it's ok just redefining it). Take your connect function out of the variable, and just put it in the method in a fashion similar to the construct method I posted above - means you can do some real error reporting on mysql_connect.

vilify
01-15-2012, 09:37 AM
omg thank you :thumbsup:. your way works perfect! +rep +thanks

I am fairly new to classes/functions. Just going off what I learned on killerphp

suggestions on anywhere to help me better understand some OOP?

BluePanther
01-15-2012, 01:08 PM
Thanks, glad I could help :thumbsup:

The PHP manual is a great resource. It has definitions of every function and datatype available in PHP. They have a decent explanation of their classes and objects in there too.

It's just about thinking about OOP logically. Objects should represent different areas and group related tasks together. If you think about it in real world scenarios, your toaster (object) can cook bread, but shouldn't cook beans. Your hob (object) can cook beans, but shouldn't toast bread. But, your toaster and hob can be used to make beans on toast :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum