...

View Full Version : if statement (Solved)



Zangeel
01-19-2008, 12:11 PM
Hi!

I'm using a login script based no
http://php.about.com/od/finishedphp1/ss/php_login_code.htm

I want to know what to use for the conditional statement so i can do something like:


<?php
if(USER IS LOGGED IN) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>


any help?
I just need to know what to put to make the condition.

adze
01-19-2008, 12:44 PM
Well you need to check to see if the submit button has been pressed.


<?php

if(isset($_POST['Submit'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>

Hope that helps

abduraooft
01-19-2008, 01:13 PM
I think the answer is there in the same site.
http://php.about.com/od/finishedphp1/ss/php_login_code_6.htm

Zangeel
01-19-2008, 01:59 PM
not really.
the member page checks for a cookie and if you're not logged in it simply redirects you, not exactly what im looking for.

and in case i wasnt to clear what i want is

theres a column on my page, it says log in, and has the log in form.
doesn't make much sense to have the login form if you're already logged in.
so i want an else there so i can remove it and put a message instead:thumbsup:

adze
01-19-2008, 07:17 PM
well like i said above, this should fix your problem!!


<?php

if(isset($_POST['Submit'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>

vinternet
01-19-2008, 08:17 PM
i think the solution is


<?php
if(USER IS LOGGED IN) {
echo "youre logged in";
} else {
echo "your html code to login";
}
?>


in your html code change the double quotes in single quotes or just let them away

Zangeel
01-19-2008, 11:50 PM
well like i said above, this should fix your problem!!


<?php

if(isset($_POST['Submit'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>


No CiGar, tested. --fFfailed---

the-dream
01-19-2008, 11:52 PM
There was a small error with the code, try this:


<?php

if(isset($_POST['submit'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>

matak
01-20-2008, 12:00 AM
You need to have some way to check if your user is logged in. If you just say


if(isset($_POST['submit']) {
#logged in code
}
else {
#not logged in code
}

It will show #logged in code just one page after user hits submit button.

That's why cookies are used, beacouse in cookies or php sessions you store data on users computer.


Then when user closes browser (if he doesn't delete cookies of course) when he get's back on your blog he will be logged in.

Try php.net/predefined_variables


Here's how i tried using sessions for registration website



<?php
error_reporting(E_ALL);
session_start();

if (isset($_POST['registration'])):
print "Do user check function...";

$to = "matak@mail.com"; //
$from = "";
$ime = $_POST['nickname'];
$subject = "**** registration";
$sadrzaj = "Mail šalje: " . $ime ;
$header = "From: $from";
mail($to,$subject,$sadrzaj,$header) or die("PHP ERRORRR");

elseif (!isset($_POST['check'])):
print file_get_contents('html/rules.html');
$_SESSION['session_on'] = 1;
elseif($_POST['check'] == "on" && empty($_SESSION['session_on'])):
print file_get_contents("html/cookies.html");
elseif($_POST['check'] == "on" && $_SESSION['session_on'] == TRUE):
print file_get_contents("html/registration.html");
endif;

?>

Zangeel
01-20-2008, 01:40 AM
Not working guys.

Let me show you exactly what im working with here,

Step 1 I added the sql

CREATE TABLE users (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(60), password VARCHAR(60))

Step 2 i created registration



//This code runs if the form has been submitted
if (isset($_POST['submit'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match.');
}

// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}

// now we insert it into the database
$insert = "INSERT INTO users (username, password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
?>


<h1>Registered</h1>
<p>Thank you, you have registered - you may now login</a>.</p><?php
}
else
{
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form>

<?php
}
?>

Step 3 login

<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: members.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database.<a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}else
{

// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);

//then redirect them to the members area
header("Location: members.php");
}
}
}
else
{

// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}

?>

Then other irrelevant things.
But then I wanted to add a login form on the header of my site.
So I used this..


<p style="margin-top: 0; margin-bottom: 0" align="center"><font color=red><font size=1>Login, or <a href=/register.php>Register</a><font color=black><br><br>
<?php

if(isset($_POST['submit'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>
</p>


yet, it's not working ;s

matak
01-20-2008, 05:00 AM
you should check for COOKIE not for POST..

Zangeel
01-20-2008, 12:12 PM
=SOLVED=

Solution:

<?php

if(isset($_COOKIE['ID_my_site'])) {
echo "youre logged in";
} else {
?>
<form name="login" method="POST" action="../login.php">
<table border="0">
<tr><td><font size=1>User:</font></td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td><font size=1>Pass:</font></td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<?php
}
?>

Champion Work.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum