SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session state help

    i am having trouble with getting my login page to work could someone help me figure out where i am going wrong with this code please since it don't redirect to the other page once logged in and don't store the session thanks.
    PHP Code:
    <?php
    session_start
    ();
    include(
    'mysql.php');
    mysql_connect("$host""$user""$passwd");
    mysql_select_db("mdb_sn027");
    if(isset(
    $_SESSION['username']))
    {
        die(
    "You are already logged in!");
    // That bit of code checks if you are logged in or not, and if you are, you can't log in again!
    if(isset($_POST['submit']))
    {
    $myusername=$_POST['usename']; 
    $mypassword=$_POST['pass']; 

    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);

    $sql="SELECT * FROM user WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);

    //If result matched $myusername and $mypassword,table is 1 row
    if($count==1){

    $_SESSION['myusername'] = $myusername
    header("location: ........."); 

    else {  
        if (isset(
    $_SESSION['myusername'])) {
            unset(
    $_SESSION['myusername']);
        }
        echo 
    "Wrong Username or Password";  
    }  
    }
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    One tip is you might need session_write_close() just before the header() redirect.

    $_POST['usename']
    is that supposed to be username ?

    You've said what it didn't do but what is it actually doing?
    - Robert

  3. #3
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the textbox i named usename
    when you type in the password and username it redirects to a new page but there is also a webpage where you have to be logged on to view it. when you click to access after password and username have been accepted it still redirects you to the login page.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    350
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I assume your database access is not having any problems ?
    Is it possible the 'die' statement is preventing the header() redirect ?

    As a separate matter, you should consider using 'mysqli' as 'mysql' functions are deprecated now.
    Tim Dawson
    Isle of Mull, Scotland

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,032
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Is the "Home" script index.php if so move the session_start() to the start of the index.php
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Right;

    1: Consider updating the script to use mysqli or PDO.
    2: Location has a capital L.
    3: the if inside your outer Else clause will never evaluate to true.
    4: You're not detecting the session because of the following:
    Code:
    if(isset($_SESSION['username'])) 
    ...
    $_SESSION['myusername'] = $myusername;
    ~One of these things is not like the other....~ (come on, everybody sing with me!)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •