Hi, I am new to php and I am totally lost on what steps I need to take in order to create a website that will allow a user to login with an email address and password or subscribe or unsubscribe. if they choose to subscribe then they should be directed to a contact form to enter additional information and if they unsubscribe they should receive an email stating that they successfully unsubscribed. They should also be given the option of logging out and destroying the session...can someone please help me or lead me in the right direction. Thank you so much for you help. This is what I have so far:

<html> 

<head>
<title>A Girl's World</title>
</head>
<form name="input" action="p5direct.php" method="post">
<body background="kenya.jpg">
<p align="center"><font size="5">The World According to a Diva</font></p>
Please log in to access your subscription:<br>
Email Address: <input type="text" name="email" /><br />
Password: <input type="password" name="pwd" /><br />
<input type='submit' name='login' value='login'><br>
Would you like to Subscribe to our News service:<br>
<input type="radio" name="action" value="subscribe" /> Subscribe
<input type="radio" name="action" value="unsubscribe" /> Unsubscribe<br />
<input type='submit' name='submit' value='submit'>
</body>
</html>


<?php
echo "<h1>Subscribe/Unsubscribe</h1>";

if(isset($_POST['submit'])) 
{
$emailad = $_POST['email'];
$passwd = $_POST['password'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$phonenumb = $_POST['phonenum'];
$birthdate = $_POST['dob'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$submit = $_POST['submit'];
$subchoice = $_POST['subs'];

$server = 'localhost';
$user = 'root';
$datapassword = '';
$database = 'km';
$table_name = 'subscribers';

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";

$connect = mysql_connect($server, $user, $datapassword) or die ("Could Not Connect!");
mysql_select_db($database, $connect) or die ("Could Not Connect!");

        $SQLcmd = mysql_query("Select email FROM subscribers WHERE email='$email'");
        $count = mysql_num_rows($SQLcmd);

        if ($count!=0)

        {
           die("That email has  already been used ");
         }  

    if ($subchoice == subscribe)
    {
       if (preg_match($pattern, $email))
       { 
       echo "<font color='red'>The Email Field contains invalid characters.</font><br>"; 
       } 

          elseif (strlen($password)<6)
        {      
       echo "<font color='red'>Password must be 6 or more characters</font>";
        }
          elseif (!preg_match("$ddmmyyy", $date)) 
          {
         echo " <font color='red'>Invalid format for DOB</font>";
          }
          else{
         $queryreg = mysql_query("

        INSERT INTO users VALUES ()

        ");

        die("You have been registered!");
       }


     }

  }
else
    echo "<font color='red'>please fill in <b>all</b> fields!</font>";



?>

Hey shawtyred74,

Your second code isn't properly wrapped in code tags. The problem is the opening brace to the ending code wrapper.

Instead of [/CODE], you have /CODE]

[<?php
echo "<h1>Subscribe/Unsubscribe</h1>";

if(isset($_POST['submit'])) 
{
$emailad = $_POST['email'];
$passwd = $_POST['password'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$phonenumb = $_POST['phonenum'];
$birthdate = $_POST['dob'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$submit = $_POST['submit'];
$subchoice = $_POST['subs'];

$server = 'localhost';
$user = 'root';
$datapassword = '';
$database = 'km';
$table_name = 'subscribers';

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";

$connect = mysql_connect($server, $user, $datapassword) or die ("Could Not Connect!");
mysql_select_db($database, $connect) or die ("Could Not Connect!");

$SQLcmd = mysql_query("Select email FROM subscribers WHERE email='$email'");
$count = mysql_num_rows($SQLcmd);

if ($count!=0)

{
die("That email has already been used ");
} 

if ($subchoice == subscribe)
{
if (preg_match($pattern, $email))
{ 
echo "<font color='red'>The Email Field contains invalid characters.</font><br>"; 
} 

elseif (strlen($password)<6)
{ 
echo "<font color='red'>Password must be 6 or more characters</font>";
}
elseif (!preg_match("$ddmmyyy", $date)) 
{
echo " <font color='red'>Invalid format for DOB</font>";
}
else{
$queryreg = mysql_query("

INSERT INTO users VALUES ()

");

die("You have been registered!");
}


}

}
else
echo "<font color='red'>please fill in <b>all</b> fields!</font>";



?>

here is another one I am trying...I am so cinfused on which direction to go. I know a very little bout php so if anyone can help me please

<?php 
session_start(); 
ob_start();
?>

<html>
<body>
<?php

if(isset($_POST['login'])) 
{
$login = $_POST['login'];

$emailad = $_POST['email'];
$passwd = $_POST['password'];
$server = 'localhost';
$user = 'root';
$datapassword = '';
$database = 'km';
$table_name = 'subscribers';

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
$pattern2 = '/[A-Z[:space:].|@|-|,|$|!|?|%|!]/';

if(session_id()==$_SESSION["userid"]){
if ($emailad && $passwd)	{
  $connect = mysql_connect($server, $user, $datapassword) or die ("Could Not Connect!");
  mysql_select_db($database, $connect) or die ("Could Not Connect!");
  
  $SQLcmd = "SELECT * FROM $table_name WHERE username='$username' AND password='$passwd'"; 
  $query = mysql_query($SQLcmd);
  $numrows = mysql_num_rows($query);
  if($numrows != 0)	{
   while ($row = mysql_fetch_array($query))
   {  $dbemail = $row['email'];
      $dbpassword = $row['password'];  }
   if ($emailad == $dbemail && $passwd == $dbpassword)	{   
     //$_SESSION['username'] = $username; 
     $_SESSION['unerror']="";
     //header( "Location: program1.php" );  
	 }
   else {  
     echo "<font color='red'>Invalid Password, Please Try Again!!!</font>";
	 	} }
 else  {
     echo "<font color='red'>That user does not exist!!!</font>";
     	} }
}else	{
  echo  "<font color='red'>User Must be logged in!!!</font>";
     	}	
		
?>

<p>
<html>
<form action='welcome.php' method='POST'>
      <table>
	        <tr>
			   <td>
			   Enter Email Address:
			   </td>
			   <td>
			   <input type='text' name='email'>
			   </td>
			</tr>  
			<tr>
			   <td>
			  Enter a Password:
			   </td>
			   <td>
			   <input type='password' name='password'>
			   </td>
			</tr>   
						
	   </table>
	   <p>
	   <input type='submit' name='submit' value='login'><br>
	   Would you like to Subscribe to our News service:<br>
<input type="radio" name="action" value="subscribe" /> Subscribe
<input type="radio" name="action" value="unsubscribe" /> Unsubscribe<br />
<input type='submit' name='submit' value='submit'>
	   
</form>

my if else statements dont seem to work correctly in the last code I posted

Member Avatar for diafol

You've got multiple <html> tags and your html is deprecated.

thanx so much...I didnt relaize that so I corrected that but I am getting a parse error at line 85 and I have no code past line 83 so i am totally confused.. :(

When you get an error on the last line of the program (or even beyond as in this case),
it means that PHP has gone through the module expecting to find a closing brace/bracket (or sometimes a closing quote) and it didn't find it.

In this case, it looks as if the IF statement on line 10 needs a closing brace (maybe on line 13). If you consistently indent your code, it makes it easier to find it.

Thank you, I think I closed all my curly braces but I a still getting an error. Do you happen to see any errors I am missing...

Thank you, I think I closed all my curly braces but I a still getting an error. Do you happen to see any errors I am missing...

what is the error currently and your current code also?

my error message is saying Notice: Undefined index: userid in C:\wamp\www\phpclass\welcome.php on line 25

and my code is

<?php
echo "<h1>Subscribe/Unsubscribe</h1>";

if(isset($_POST['submit'])) 
{

$emailad = $_POST['email'];
$passwd = $_POST['password'];
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$phonenumb = $_POST['phonenum'];
$birthdate = $_POST['dob'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$gender = $_POST['sex'];
$marital = $_POST['status'];
$submit = $_POST['submit'];
$subchoice = $_POST['action'];

$server = 'localhost';
$user = 'root';
$datapassword = '';
$database = 'km';
$table_name = 'subscribers';

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";

$connect = mysql_connect($server, $user, $datapassword) or die ("Could Not Connect!");
mysql_select_db($database, $connect) or die ("Could Not Connect!");

        $SQLcmd = mysql_query("Select email FROM subscribers WHERE email='$email'");
        $count = mysql_num_rows($SQLcmd);

        if ($count!=0)
		
		{
		   die("That email has  already been used ");
		 }  
		 
	if ($subchoice == subscribe)
	{
	   if (preg_match($pattern, $email))
	   { 
	   echo "<font color='red'>The Email Field contains invalid characters.</font><br>"; 
	   } 

	      elseif (strlen($password)<6)
        {	   
	   echo "<font color='red'>Password must be 6 or more characters</font>";
	    }
          elseif (!preg_match("$ddmmyyy", $date)) 
		  {
		 echo " <font color='red'>Invalid format for DOB</font>";
		  }
		  else{
		 $queryreg = mysql_query("

        INSERT INTO subscibers VALUES ('$emailad','$passwd','$fname','$lname','$phonenumb','$birthdate','$city','$state','$zip')

        ");

        die("You have successfully subscribed, please check your email!");
       }


     }

  }
else
    echo "<font color='red'>please fill in <b>all</b> fields!</font>";
	


?>

I am trying to have a session in my code so I can use session destroy when the user logs out

is this code for welcome.php? I cannot find userid anywhere in the code!
It is strange that PHP complains about something non existant!

oh I am sorry I copied the other code I was trying....here is my code:

<?php 
session_start(); 
ob_start();
$_SESSION["userid"]=session_id();
?>



<?php

if(isset($_POST['login'])) 
{
$login = $_POST['login'];

$emailad = $_POST['email'];
$passwd = $_POST['password'];
$server = 'localhost';
$user = 'root';
$datapassword = '';
$database = 'km';
$table_name = 'subscribers';

$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
$pattern2 = '/[A-Z[:space:].|@|-|,|$|!|?|%|!]/';

if(session_id()==$_SESSION["userid"]){
if ($emailad && $passwd)	{
  $connect = mysql_connect($server, $user, $datapassword) or die ("Could Not Connect!");
  mysql_select_db($database, $connect) or die ("Could Not Connect!");
  
  $SQLcmd = "SELECT * FROM $table_name WHERE email='$emailad' AND password='$passwd'"; 
  $query = mysql_query($SQLcmd);
  $numrows = mysql_num_rows($query);
  if($numrows != 0)	{
   while ($row = mysql_fetch_array($query))
   {  $dbemail = $row['email'];
      $dbpassword = $row['password'];  }
   if ($emailad == $dbemail && $passwd == $dbpassword)	{   
     //$_SESSION['username'] = $username; 
     $_SESSION['unerror']="";
     //header( "Location: program1.php" );  
	 }
   else {  
     echo "<font color='red'>Invalid Password, Please Try Again!!!</font>";
	 	} }
 else  {
     echo "<font color='red'>That user does not exist!!!</font>";
     	} }
}else	{
  echo  "<font color='red'>User Must be logged in!!!</font>";
     	}	
	}	
?>

<p>
<html>
<form action='welcome.php' method='POST'>
      <table>
	        <tr>
			   <td>
			   Enter Email Address:
			   </td>
			   <td>
			   <input type='text' name='email'>
			   </td>
			</tr>  
			<tr>
			   <td>
			  Enter a Password:
			   </td>
			   <td>
			   <input type='password' name='password'>
			   </td>
			</tr>   
						
	   </table>
	   <p>
	   <input type='submit' name='login' value='login'><br>
	   Would you like to Subscribe to our News service:<br>
<input type="radio" name="action" value="subscribe" /> Subscribe
<input type="radio" name="action" value="unsubscribe" /> Unsubscribe<br />
<input type='submit' name='submit' value='submit'>
	   
</form>

I cannot see what is wrong with userid yet. Ignore what I said in unedited post!

What is function of this line? AFAIK it is useless!

if(session_id()==$_SESSION["userid"])

EDIT:
I would make an Authentication class and in it put method that check if user is login then do something like

if($AuthObj->isLoggedIn()){
   //user is logged in do stuffs
}else{
    //redirect user to login page or out errors
}

I added that line because I was trying to create a session so when the user logs out it will destroy the session...was I headed in the wrong direction

thank you very much I am going to try that

I added that line because I was trying to create a session so when the user logs out it will destroy the session...was I headed in the wrong direction

Just use:

if(isset($_SESSION['userid'])){
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.