Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-13-2011, 04:46 PM   PM User | #16
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Add session_start to the top of the script. This one is still wrong: $_SESSION("Login")=$Prename;, as you cannot assign to a result of a function call (remember, $_SESSION("login") is trying to map $_SESSION to a callable function which will fail). Those must be changed to square braces.

Also, best to make sure your variables exist:
PHP Code:
$Prename=isset($_POST['Prename']) ? trim($_POST['Prename']) : '';
$Surname=isset($_POST['Surname']) ? trim($_POST['Surname']) : ''
If the script is accessed without these two post variables, they will default to nothing without triggering a notice.

Watch the use of the operators as well. These are deceiving (though correct here as well), since both exist in PHP:
PHP Code:
$Prename=="firstname1" && $Surname=="lastname1" or $Prename=="firstname2" && 
Notice your use of && and OR. PHP has all four ops for this: '&&' > '||' > '=' > 'AND' > 'OR', if you can read that ok. This works no problem since all levels of and override that of OR. This can be found on the precedence list here: http://php.ca/manual/en/language.ope...precedence.php

Notice if you switched things up:
PHP Code:
$Prename == "firstname1" AND $Surname == "lastname1" || $Prename == "firstname2"... 
The evaluation changes from logical: if ((prename AND surname) OR (prename.....) to: if (prename AND (surname OR prename) AND....), which is definitely not what you want.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 04-13-2011 at 05:01 PM.. Reason: OP modified to remove sensitive, reflected here
Fou-Lu is offline   Reply With Quote
Old 04-13-2011, 04:52 PM   PM User | #17
munkeyboy
New Coder

 
Join Date: Apr 2011
Posts: 36
Thanks: 0
Thanked 3 Times in 3 Posts
munkeyboy is on a distinguished road
You omitted the session_start() function so your variables are getting lost during the page refresh. Also your assignment operators ie $Surname==""; should only have one =.
munkeyboy is offline   Reply With Quote
Old 04-13-2011, 05:02 PM   PM User | #18
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by munkeyboy View Post
You omitted the session_start() function so your variables are getting lost during the page refresh. Also your assignment operators ie $Surname==""; should only have one =.
Yep, good catch I completely missed that.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 04-18-2011, 03:55 PM   PM User | #19
Elsean
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Elsean is an unknown quantity at this point
I do have the session_start() on the first line of my page, It just says<?php session_start(); ?> is that enough?
Elsean is offline   Reply With Quote
Old 04-19-2011, 04:27 AM   PM User | #20
munkeyboy
New Coder

 
Join Date: Apr 2011
Posts: 36
Thanks: 0
Thanked 3 Times in 3 Posts
munkeyboy is on a distinguished road
Yes that is sufficient, however that wasn't in the code you posted so as far as we could tell it wasn't there. Once you fix the double equals (==) in your assignment operators then your code should work.

Edit:
Assignment Operators and Comparison Operators
munkeyboy is offline   Reply With Quote
Old 04-19-2011, 07:40 PM   PM User | #21
Elsean
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Elsean is an unknown quantity at this point
I did what you said, but now it thinks $_SESSION['LoginTester']==True, (which I haven't even set yet!) when I first enter the page. The updated script is:

PHP Code:
<?php
$Prename
=$_POST['Prename'];
$Surname=$_POST['Surname'];

if (
$_SESSION['LoginTester']==True)
  {
  echo 
"Hello, " $_SESSION['Login'];
  }
else
  {
  if (
$Surname=="Surname")
    {
    
$Surname="";
    }

  if (
$Prename=="Prename")
    {
    
$Prename="";
    }

  if (
$Prename <> "" && $Surname <> "")
    {
    if (
//the names)
      
{
      
$_SESSION["LoginTester"]=True;
      
$_SESSION["Login"]=$Prename " " $Surname;
      }
    else
      {
      echo 
"<u>Login</u><br>You are not a registered user";
      }
    }
  else
    {
    if (
$Prename=="//another name" && $Surname=="")
      {
      
$_SESSION["LoginTester"]=True;
      
$_SESSION["Login"]=$Prename;
      }
    else
      {
      echo 
"<u>Login</u><br>Please login<br><form action='Stories.php' method='post'><input type='text' value='Prename' name='Prename'><br><input type='text' value='Surname' name='Surname'><br><input type='Submit' value='Submit'></form>";
      }
    }
  }
?>
It basically doesn't give me a chance to imput my login and just runs echo "Hello, " . $_SESSION['Login'] but the Session Variable 'login' doesn't exist so it comes up blank
Elsean is offline   Reply With Quote
Old 04-21-2011, 06:54 PM   PM User | #22
munkeyboy
New Coder

 
Join Date: Apr 2011
Posts: 36
Thanks: 0
Thanked 3 Times in 3 Posts
munkeyboy is on a distinguished road
Try closing your browser then reopening it before trying to access the form again. It's very possible that your session data is being saved and without having a logout script in place the easiest way to log out is to close and re-open the browser. If that resolves the issue then you will need to build a method of logging out of the session. The session_destroy function would be a good place to start.
munkeyboy is offline   Reply With Quote
Old 04-22-2011, 05:53 PM   PM User | #23
Elsean
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Elsean is an unknown quantity at this point
considering a logout script, is there any way I could get a button or hyperlink to run the session_destroy function.
Elsean is offline   Reply With Quote
Old 04-23-2011, 05:10 AM   PM User | #24
munkeyboy
New Coder

 
Join Date: Apr 2011
Posts: 36
Thanks: 0
Thanked 3 Times in 3 Posts
munkeyboy is on a distinguished road
There are several ways you could go about it, the simplest method would be to have a single (separate) page that only had the session_destroy function in it, then by linking to that page you would be able to logout.
munkeyboy is offline   Reply With Quote
Old 04-23-2011, 10:40 AM   PM User | #25
Elsean
New to the CF scene

 
Join Date: Apr 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Elsean is an unknown quantity at this point
wouldn't that only work with a button though?
Elsean is offline   Reply With Quote
Reply

Bookmarks

Tags
error, login script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:35 AM.


Advertisement
Log in to turn off these ads.