0

i know the best approach when programming is not to disable errors,i have been getting a series of errors ever since i started making my simple web forum,i have tried to get ride of some but i am having a problem with some,the undefined variable is the most common error and it seems like i have to declare some like A=''; then A=values; for example but i have no idea how i can tackle this one below

Notice: Undefined variable: act in C:\xampp\htdocs\mysite\forum part two\login.php on line 74

the code on line 74 is this

case "login";

and my other code where there is login is this in the login.php

switch($act){

default;
index();
break;

case "login";
login();
break;

}

my code on the login.php is this

<?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", "root", "nokiae71");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("forumStructure", $connect);
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;
}
?> 
theuserkaps
  • 300
  • 2
  • 6
  • 15
  • Do you define $act somewhere ? What is it ? – Denys Séguret Dec 05 '12 at 08:04
  • check http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – NullPoiиteя Dec 05 '12 at 08:06
  • ok let me add the code on the login.php – theuserkaps Dec 05 '12 at 08:09
  • 4
    A man is smoking a cigarette and blowing smoke rings into the air. His girlfriend becomes irritated with the smoke and says, “Can’t you see the warning on the cigarette pack? Smoking is hazardous to your health!” To which the man replies, “I am a programmer. We don’t worry about warnings; we only worry about errors.” – Ron van der Heijden Dec 05 '12 at 08:10
  • @Bondye :D. Anyway, theuserkaps, you need to initialize/declare the variables first. – art2 Dec 05 '12 at 08:12

3 Answers3

1

probably you didn't define $act variable..

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

and in case statement do not use semicolon

case "whatever":
               ^
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
1

Add this on top of your code:

$act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : null;

It might be better even if you put all of these global things like session_start(); and $act = ... all into a file named gloabls.inc.php for example, then including that into the other pages.

Mahdi
  • 9,247
  • 9
  • 53
  • 74
1

This most likely means you didn't declare the variable $act.

You can handle this by adding a condition around your switch statement:

if (isset($act)) {
    switch($act) {
        // Do what you need here
    }
}
else {
    echo '$act is not set';
}

This way, you can use that else condition to handle cases where $act is empty or isn't set without throwing warnings.

Stegrex
  • 4,004
  • 1
  • 17
  • 19