Jump to content

Recommended Posts

[code]    <?php
if($_SESSION['myemail']){
echo "Logged in as <strong>$_SESSION['fname'] $_SESSION['sname']</strong>. <br />
Click here to log out";
}else{
echo '<a href="loginotd.php" target="mainpage">Login</a><br />
        <a href="javascript:void window.open('register.php','Checkout','status=no, scrollbars=1, height=790, width=560')">Register</a>';}
?>[/code]

I cant see it... I'm going blind i swear.

Snooble
Link to comment
https://forums.phpfreaks.com/topic/36252-whats-wrong-with-this-if-statement/
Share on other sites

You're better off using something like isset()

[code]<?php
if (isset($_SESSION['myemail'])){
  echo "Logged in as <strong>$_SESSION['fname'] $_SESSION['sname']</strong>. <br />\nClick here to log out";
}
else{
  echo <<<HTML
  <a href="loginotd.php" target="mainpage">Login</a><br />
  <a href="javascript:void window.open('register.php','Checkout','status=no, scrollbars=1, height=790, width=560')">Register</a>
HTML;
}
?>[/code]

[b]Edit:[/b] Code corrected

Regards
Huggie
You must have [url=http://uk.php.net/manual/en/function.session-start.php]session_start()[/url] at the top of every page that uses sessions.  It has to appear before anything gets output to the browser.

A quick read up on [url=http://uk.php.net/manual/en/ref.session.php]PHP's Sessions[/url] should help out here

Regards
Huggie
[quote author=Snooble link=topic=124619.msg516659#msg516659 date=1170116881]
I am learning... honestly. Thank you so much
[/quote]

My advice in that case is to always have the PHP manual open beside your code.  As far as I'm concerned, it's the best manual of any programming language I've ever worked with.

The main page is here: http://uk.php.net/manual/en/index.php (UK Mirror)

And from there you can type in any function name in the box at the top right of the page and be taken straight to the details of it.

Regards
Huggie
"what do i put beneath session_start? "

The rest of your code.



Huggie: you know you can even just do php.net/function and it goes right to it? (I'm a sucker for any shortcuts)
so php.net/array is for arrays, php.net/substr, etc.
I'm reading it as we speak. But it all seems so confusing :S.

I have the login box working. But i dont know what happens after that.

When i type session_start();

if i was logged in would it be able to post the firstname and second name that are stored in the database?

Thank you Huggy.

Snooble
[quote author=Snooble link=topic=124619.msg516667#msg516667 date=1170117265]
if i was logged in would it be able to post the firstname and second name that are stored in the database?
[/quote]

On your login page once you've authenticated a user, you can assign their details into session variables, then by placing session_start() at the top of any of your other pages on your site, you can retrieve those variables using the global $_SESSION array...

[b]login.php[/b]
[code]<?php
// Start the session
session_start();

// Get my user details from the form
$user = $_GET['frmUser'];
$pass = $_GET['frmPass'];

// My database query to authenticate the user would go here
$sql = "SELECT real_name FROM users WHERE ....."; // get the idea

// Process the query here with mysql_query()
$result = mysql_query($sql) or die ("Unable to execute $sql: " . mysql_error());

// Put the persons real name into a session variable
$details = mysql_fetch_array($result, MYSQL_ASSOC);
$_SESSION['name'] = $details['real_name'];
?>[/code]

Now to echo that on page two all I do is this:

[b]page2.php[/b]
[code]<?php
// Start the session again
session_start();

// Check for username
if (isset($_SESSION['name'])){
  echo "Welcome " . SESSION['name']
}
else {
  header('Location: login.php');
}
?>[/code]

Regards
Huggie

I will give it a go tomorrow morning. Thank you very much.

Good night. If this will help:

"login.php"

[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style21 {font-family: ADMUI3Lg, serif, monospace, cursive; font-weight: bold; font-size: 16px; color: #FFFFFF; }
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.style22 { color: #000000;
font-size: 14px;
}
.style25 {font-size: 9px; color: #fbedc7; }
.style26 {font-size: 14px}
.style27 {color: #fbedc7}
.style28 {color: #000000}
.style29 { font-size: 10px;
color: #FF0000;
}
.style30 {font-size: 9px}
.style31 { font-size: 11px;
color: #CCCCCC;
}
.style32 {font-size: 1px}
.style34 {color: #000000; font-size: 12px; }
.style36 {font-size: 13px}
.style37 {
color: #FFFFFF;
font-size: 1px;
}
-->
</style>
</head>

<body>
<form method="post" action="checklogin.php"><table width="100%" height="100%" border="0">
  <tr>
    <td height="25" background="Untitled-4.gif"><div align="center"><span class="style21">Login </span></div></td>
  </tr>
  <tr>
    <td valign="top"><table width="100%" height="0%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="5" colspan="4" valign="top"><span class="style37">.</span></td>
        </tr>
      <tr>
        <td height="21" colspan="4" valign="top" background="fbedc7.gif">&nbsp;</td>
      </tr>
      <tr>
        <td width="552" height="56%" valign="middle" background="fbedc7.gif"><div align="right"><span class="style22"> Email Address </span><span class="style26"><br />
                <span class="style27">s</span><br />
                  <span class="style28">Passwor<span class="style22">d</span><span class="style22"> </span><span class="style22"> </span> </span></span></div></td>
        <td width="5" valign="top" background="fbedc7.gif">&nbsp;</td>
        <td width="375" valign="top" background="fbedc7.gif"><input type="text" name="myemail" />
          <br />
          <span class="style25"> s </span> <br />
          <input type="password" name="mypassword" /></td>
        <td valign="top" background="fbedc7.gif">&nbsp;</td>
      </tr>
      <tr>
        <td height="12%" align="center" valign="middle" background="fbedc7.gif"><div align="right"><span class="style30"><span class="style31">Remember Me</span>
                  <input name="radiobutton" type="radio" value="radiobutton" />
        </span></div></td>
        <td height="12%" colspan="2" align="center" valign="middle" background="fbedc7.gif"><span class="style29"><a href="retrievepassword.php">Forgot Your Password?</a></span></td>
        <td width="294" valign="top" background="fbedc7.gif"><div align="center">
            <input type="submit" name="Submit" value="Login">
        </div></td>
      </tr>
      <tr>
        <td height="8" colspan="4" align="center" valign="middle" bgcolor="#FFFFFF"><span class="style37">a</span></td>
      </tr>
      <tr>
        <td height="25" colspan="4" align="center" valign="middle" background="Untitled-4.gif"><span class="style21">Security Notice </span></td>
      </tr>
      <tr>
        <td height="4" colspan="4" align="center" valign="middle" bgcolor="#FFFFFF"><span class="style32"> a </span></td>
      </tr>
      <tr>
        <td height="110" colspan="4" align="center" valign="middle" background="fbedc7.gif" bgcolor="#FFFFFF"><div align="left" class="style34">
            <div align="center"><span class="style27">.</span><br />
              <span class="style36">1. Look for http://www.onetimedeals.co.nr in the URL box.<br />
              <br />
              2. Never give out your password to anyone. We will never ask for it, EVER.<br />
              <br />
              3. If you feel you are not secure on our site at any time please ring us immeadiatly.</span><br />
              <br />
              <br />
            </div>
        </div></td>
      </tr>
      <tr>
        <td height="8" colspan="4" align="center" valign="middle" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
    </table>
      <br />
      <br />
      <br />
      <br /></td>
  </tr>
</table></form>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</body>
</html>
[/code]

checklogin.php
[code]<?php
$host="**********"; // Host name
$username="********"; // Mysql username
$password="********"; // Mysql password
$db_name="***********"; // Database name
$tbl_name="web_members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form
$myemail=$_POST['myemail'];
$mypassword=$_POST['mypassword'];

$sql="SELECT * FROM $tbl_name WHERE Email='$myemail' and Password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myemail and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $myemail and redirect to file "login_success.php"
$_SESSION['myemail'] = $myemail;
$_SESSION['mypassword'] = $mypassword;
$_SESSION['fname'] = $fname;
$_SESSION['sname'] = $sname;
$_SESSION['addno'] = $addno;
$_SESSION['addone'] = $addone;
header("location:index.php");
}
else {
echo "Wrong Username or Password";
}
?>

[/code]

Registration Page
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Secure :: One Time Deals :: Register </title>
<style type="text/css">
<!--
.style21 {font-family: ADMUI3Lg, serif, monospace, cursive; font-weight: bold; font-size: 16px; color: #FFFFFF; }
body {
margin: 0px;
padding-top: 2px;
padding-right: 2px;
padding-bottom: 4px;
padding-left: 2px;
background-image: url(fbedc7.gif);
}
.style37 {
color: #FFFFFF;
font-size: 1px;
}
.style38 {color: #CCCCCC}
-->
</style>
</head>

<body>
<table width="100%" height="19%" border="0" background="fbedc7.gif">
  <tr>
    <td height="25" background="Untitled-4.gif"><div align="center"><span class="style21">Register</span></div></td>
  </tr>
  <tr>
    <td height="131" valign="top" background="fbedc7.gif"><table width="100%" height="0%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="100%" height="1" valign="top"><span class="style37">.</span></td>
        </tr>
      <tr>
        <td height="33" align="center" valign="middle" background="fbedc7.gif" bgcolor="fbedc7"><div align="center"><span xmlns:sitefunctions="urn:siteFunctions">&nbsp; </span><img src="shoppingbasketheader.gif" alt="Shopping Basket" xmlns:sitefunctions="urn:siteFunctions" align="absmiddle" height="24" width="215" />
        </div></td>
      </tr>
      <tr>
        <td height="8" valign="top" bgcolor="#FFFFFF"><div align="center"><span class="style37">.</span></div></td>
      </tr>
      <tr>
        <td height="11" valign="top" background="fbedc7.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="898" background="fbedc7.gif" bgcolor="fbedc7"><div align="center"> <br />
                    <span class="style38"><?php echo $_POST["fname"]; ?> <?php echo $_POST["sname"]; ?>, Welcome To One Time Deals</span><br />
              </div>
                <br />
<?php
$host="********"; // Host name
$username="********"; // Mysql username
$password="*****"; // Mysql password
$db_name="*******"; // Database name
$tbl_name="web_members"; // Table name
// Connect to server and select databse.
echo '<form action="index.php" method="post">
<input type="hidden" name="email" value="'.$_POST['email'].'">
<input type="hidden" name="fname" value="'.$_POST['fname'].'">
<input type="hidden" name="sname" value="'.$_POST['sname'].'">
<input type="hidden" name="pass" value="'.$_POST['pass'].'">
<input type="hidden" name="passtwo" value="'.$_POST['passtwo'].'">
<input type="hidden" name="llno" value="'.$_POST['llno'].'">
<input type="hidden" name="mobno" value="'.$_POST['mobno'].'">
<input type="hidden" name="addno" value="'.$_POST['addno'].'">
<input type="hidden" name="addone" value="'.$_POST['addone'].'">
<input type="hidden" name="addtwo" value="'.$_POST['addtwo'].'">
<input type="hidden" name="addthree" value="'.$_POST['addthree'].'">
<input type="hidden" name="city" value="'.$_POST['city'].'">
<input type="hidden" name="other" value="'.$_POST['other'].'">
<input type="hidden" name="country" value="'.$_POST['country'].'">
<input type="hidden" name="noc" value="'.$_POST['noc'].'">
<input type="hidden" name="ccno" value="'.$_POST['ccno'].'">
<input type="hidden" name="expdate" value="'.$_POST['expdate'].'">
<input type="hidden" name="cvv" value="'.$_POST['cvv'].'">
<input type="image" src="register.gif" name="submit" alt="Log In" value="Log In" />';
mysql_connect("$host", "$username", "$password") or die ("cannot connect: " . mysql_error());
mysql_select_db("$db_name") or die("cannot select DB: " . mysql_error());
$sql = "INSERT INTO web_members (Email, Password, Firstname, Surname, AddNo, AddOne, AddTwo, AddThree, City, Country, Other Country, NOC, CCNO, ExpDate, ExpDatey, CVV) VALUES ('".$_POST['email']."', '".$_POST['pass']."', '".$_POST['fname']."', '".$_POST['sname']."', '".$_POST['addno']."', '".$_POST['addone']."', '".$_POST['addtwo']."', '".$_POST['addthree']."', '".$_POST['city']."', '".$_POST['country']."', '".$_POST['other']."', '".$_POST['noc']."', '".$_POST['ccno']."', '".$_POST['expdate']."', '".$_POST['cvv']."')";
mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());
?>
                </td>
            </tr>
          </table>
          <div align="center"></div></td>
      </tr>
      <tr>
        <td height="19" align="center" valign="middle" background="fbedc7.gif" bgcolor="#FFFFFF"><span class="style37">a</span></td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</body>
</html>
[/code]

Now you can have a good look at the site and what i want to achieve.

I just want the fields to be carried around the site at all times when they are logged in. I will make a log out button using session_destroy();

Thank you and i hope this information makes it clearer.

Snooble
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.