Jump to content

Recommended Posts

hi, i need help with some php problems, can anyone help?

 

problems occuring:

 

Notice: Undefined variable: act in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/register.php on line 96

 

Notice: Undefined variable: act in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/login.php on line 75

 

Notice: Undefined index: username in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/index.php on line 6

 

Notice: Undefined index: password in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/index.php on line 7

 

index.php:

<?php

//This will start a session
session_start();

$username = $_SESSION['username'];
$password = $_SESSION['password'];

//Check do we have username and password
if(!$username && !$password){
echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>";
}else{
echo "Welcome ".$username." (<a href=logout.php>Logout</a>)";
}


?>

 

login.php:

<?php
session_start();

//This displays your login form
function index(){

echo "<form action='?act=login' method='post'>"
    ."Username: <input type='text' name='username' size='30'><br>"
    ."Password: <input type='password' name='password' size='30'><br>"
    ."<input type='submit' value='Login'>"
    ."</form>";    

}

//This function will find and checks if your data is correct
function login(){

//Collect your info from login form
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];


//Connecting to database
$connect = mysql_connect("localhost", "username", "password");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("chrisrulez_membersarea");
if(!$select_db){
die(mysql_error());
}

//Find if entered data is correct

$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];

$select_user = mysql_query("SELECT * FROM users WHERE id='$id'");
$row2 = mysql_fetch_array($select_user);
$user = $row2['username'];

if($username != $user){
die("Username is wrong!");
}


$pass_check = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id'");
$row3 = mysql_fetch_array($pass_check);
$email = $row3['email'];
$select_pass = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id' AND email='$email'");
$row4 = mysql_fetch_array($select_pass);
$real_password = $row4['password'];

if($password != $real_password){
die("Your password is wrong!");
}



//Now if everything is correct let's finish his/her/its login

session_register("username", $username);
session_register("password", $password);

echo "Welcome, ".$username." please continue on our <a href=index.php>Index</a>";




}

switch($act){

default;
index();
break;

case "login";
login();
break;

}
?>

 

register.php:

<?php

//This function will display the registration form
function register_form(){

$date = date('D, M, Y');
echo "<form action='?act=register' method='post'>"
    ."Username: <input type='text' name='username' size='30'><br>"
    ."Password: <input type='password' name='password' size='30'><br>"
    ."Confirm your password: <input type='password' name='password_conf' size='30'><br>"
    ."Email: <input type='text' name='email' size='30'><br>"
    ."<input type='hidden' name='date' value='$date'>"
    ."<input type='submit' value='Register'>"
    ."</form>";

}

//This function will register users data
function register(){

//Connecting to database
$connect = mysql_connect("localhost", "username", "password");
if(!$connect){
die(mysql_error());
}

//Selecting database
$select_db = mysql_select_db("chrisrulez_membersarea");
if(!$select_db){
die(mysql_error());
}

//Collecting info
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$pass_conf = $_REQUEST['password_conf'];
$email = $_REQUEST['email'];
$date = $_REQUEST['date'];

//Here we will check do we have all inputs filled

if(empty($username)){
die("Please enter your username!<br>");
}

if(empty($password)){
die("Please enter your password!<br>");
}

if(empty($pass_conf)){
die("Please confirm your password!<br>");
}

if(empty($email)){
die("Please enter your email!");
}

//Let's check if this username is already in use

$user_check = mysql_query("SELECT username FROM users WHERE username='$username'");
$do_user_check = mysql_num_rows($user_check);

//Now if email is already in use

$email_check = mysql_query("SELECT email FROM users WHERE email='$email'");
$do_email_check = mysql_num_rows($email_check);

//Now display errors

if($do_user_check > 0){
die("Username is already in use!<br>");
}

if($do_email_check > 0){
die("Email is already in use!");
}

//Now let's check does passwords match

if($password != $pass_conf){
die("Passwords don't match!");
}


//If everything is okay let's register this user

$insert = mysql_query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
if(!$insert){
die("There's little problem: ".mysql_error());
}

echo $username.", you are now registered. Thank you!<br><a href=?act=login>Login</a> | <a href=index.php>Index</a>";

}

switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/53758-php-problems/
Share on other sites

index.php

<?php

//This will start a session
session_start();

$username = isset($_SESSION['username'])?$_SESSION['username']:'';
$password = isset($_SESSION['password'])?$_SESSION['password']:'';

 

www.php.net/isset

 

Notices do not really mean anything but that would be the proper way to assign variables to avoid the notices.

 

register.php???

echo $username.", you are now registered. Thank you!<br><a href=?act=login>Login</a> | <a href=index.php>Index</a>";

}

$act = isset($act)?$act:'';
switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?>

 

the ? and : are the ternary operators kind of like an if/else statement. If $act isset than assign $act equal to $act else assign it '' so it is atleast defined.

Link to comment
https://forums.phpfreaks.com/topic/53758-php-problems/#findComment-265699
Share on other sites

Yea you are calling $act wrong unless I missed something, try this:

 

echo $username.", you are now registered. Thank you!<br><a href=?act=login>Login</a> | <a href=index.php>Index</a>";

}

$act = isset($_GET['act'])?$_GET['act']:'';
switch($act){

default;
register_form();
break;

case "register";
register();
break;

}

?>

Link to comment
https://forums.phpfreaks.com/topic/53758-php-problems/#findComment-265714
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.