 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 7th, 2005, 11:49 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help with functions and variables
having a problem...
Code:
auth();
if ($cookies=="ok")
{
welcome();
} else loginform();
i have it authenticate then if they are logged in then to set $cookies to ok and in this block i am trying to read that variable. but am getting this error
Code:
Notice: Undefined variable: cookies in F:\localhost\users\index.php on line 19
i know globals to get variables that are outside a function but i dont know how to do the opposite.
someone please help!
http://www.deadlydesigns.net <--- My Baby
|
|

January 7th, 2005, 11:55 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
and if it helps heres index.php
Code:
<?php
include ('functions.php');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Go **** YouRseLf!</title>
</head>
<body>
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
LeftTop
</td>
<td>
<?php
auth();
if ($cookies=="ok")
{
welcome();
} else loginform();
?> </td>
</tr>
<tr>
<td>
Left Bottom
</td>
<td>
Right Rigth
</td>
</tr>
</table>
</body>
</html>
and functions.php that is included... its sorta long so get read!
Code:
<?php
$db_host = 'localhost';
$db_username= 'root';
$db_password= '';
$db_database= 'users';
$db= mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_database);
function auth()
{
$cookies = '';
global $db_host, $db_username, $db_password, $db;
if(isset($_COOKIE['username']))
{
if(isset($_COOKIE['password']))
{
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
$query = "SELECT * FROM users WHERE username = '$username' and password = '$password'";
$result = mysql_query($query, $db);
$check = mysql_num_rows($result);
if($check>0)
{
$cookies= 'ok';
}
}
}
}
function loginform()
{ ?>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
<form action="?auth=submit_login" method="post">
<table>
<tr><td colspan="2"><b>Please Login:</b></td></tr>
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td colspan="2"><input type="submit" value="Login"></td></tr>
</table>
</form>
<?php }
function submit_login()
{
global $db_host, $db_username, $db_password, $db;
if(isset($_POST['username']))
{
if(isset($_POST['password']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username = '$username' and password = '$password'";
$result = mysql_query($query, $db);
$check = mysql_num_rows($result);
if($check>0)
{
setcookie("username", $username, time()+604800); // ONE WEEK
setcookie("password", $password, time()+604800); // ONE WEEK
}
else
{
echo'<b>Username and password combination does not exist in our database</b>';
}
}
}
}
function reg()
{ ?>
<form action="?auth=submit_register" method="post" name="register" onSubmit="MM_validateForm('username','','R','email','','RisEmail','password','','R');return document.MM_returnValue">
<table>
<tr><td colspan="2"><B>Register:</B></td></tr>
<tr><td>Username:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td>Website:</td><td><input type="text" name="website"></td></tr>
<tr><td>AIM:</td><td><input type="text" name="aim"></td></tr>
<tr><td>MSN:</td><td><input type="text" name="msn"></td></tr>
<tr><td>Yahoo:</td><td><input type="text" name="yahoo"></td></tr>
<tr><td colspan="2"><input type="submit" value="Register"></td></tr>
</table>
</form>
<?php }
function submit_reg()
{
global $db_host, $db_username, $db_password, $db;
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$website = $_POST['website'];
$aim = $_POST['aim'];
$msn = $_POST['msn'];
$yahoo = $_POST['yahoo'];
if(!isset($username, $password, $email))
{
}
else
{
if ($website=="http://")
{
$website= '';
}
$query = "SELECT * FROM users WHERE username = '$username'";
$result = mysql_query($query, $db);
$check = mysql_num_rows($result);
if ($check>0)
{
echo '<b>Sorry, username already in use!<br> Please choose a different username<br></b>';
reg($username, $password, $email, $aim, $msn, $yahoo);
}
else
$query = "INSERT INTO users(username, password, email, aim, msn, yahoo) VALUES('$username', '$password', '$email', '$aim', '$msn', '$yahoo'";
$do = mysql_query($query, $db);
echo '<b>Registration Success!</b>';
setcookie("username", $username, time()+604800); // ONE WEEK
setcookie("password", $password, time()+604800); // ONE WEEK
}
}
function href($href, $text, $target=_blank)
{
echo '<a href="'.$href.'" target="'.$target.'">'.$text.'</a><br>';
}
if (isset($auth))
{
switch($_GET['auth'])
{
case 'auth':
auth();
break; case 'loginform';
loginform();
break; case 'submit_login';
submit_login();
break; case 'reg';
reg();
}}
?>
DeadlyDesigns.NET <--- My webmaster related website
|
|

January 8th, 2005, 01:17 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
do you get that error after you press submit or before?
|
|

January 8th, 2005, 01:45 AM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
just on index.php
i havent logged in yet or even registered but thats why i also have the cookies = ''; first thing in the auth() function
i _think_ i have everything right besides the cookies var
i just dont know how to make a variable be usable after the function is done executing.
like global... if you dont use global you cant use the variables outside of the function...
i wanna be able to access the varialbe after the function is done creating it and stuff
not sure on this... but i think functions delete their vars and stuff after they execute so if i am right i need to find out how to save a var
http://www.deadlydesigns.net <--- My Baby!
|
|

January 8th, 2005, 07:34 AM
|
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
try if (@$cookies = 'ok')
|
|

January 8th, 2005, 08:22 PM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
nvm... i just changed it so the auth() isnt a function anymore... works ok
but i still would like to know how to make a variable not delete itself after function execution... if anyone knows, please tell me
http://www.deadlydesigns.net <--- My Baby!
|
|

January 26th, 2005, 12:28 AM
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
update!
I figured this out...
instead of:
auth();
if($cookies==ok){
you do:
$cookies= auth();
and then test it
and you also need to add a thing in the function to return it.
at the end of the function right before the closing bracket you gotta put
return $cookies;
http://www.deadlydesigns.net <--- My Baby!
|
|
 |