SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting script into php4?

    Hi All,
    Im used to coding in PHP5, but now i need to convert this login script to PHP4... I have changed some things but still no luck. Any ideas?


    PHP Code:
    <?php
        session_start
    ();
        
    $msg_pass="";
        
    $msg_user="";
        
    $username=trim($_POST['username']);
        
    $password=trim($_POST['password']);
        if(
    $username && $password)
        {
            
    // connect to database
            
    include("includes/db_con1.php");
            echo 
    "connected";
            
    // check if the username exists
            
    $cQuery mysql_query("SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'");
            
    $rs=mysql_query($cQuery);
            if(
    $rs)
            {
                
    $count=mysql_num_rows($rs);
                if(
    $count>0)
                {
                    
    //read the data from table
                    
    $data=mysql_fetch_assoc($rs);
                    if(
    strcmp($data['password'],$ushapass))
                    {
                        
    // setting a session with a variable named valid user which stores the username
                        
    $_SESSION['validuser']=$username;
                        
                    }
                    else
                    {
                        
    $msg_pass"Please check your username and password are correct";
                    }
                }
                else
                {
                    
    $msg_user="Please check your username and password are correct";
                }
            }
            else
            {
                echo 
    "Unable to execute the query : ".mysql_error($con);
            }
        }
    // end of check
        
        
    if(isset($_SESSION['validuser']))
        {
            echo 
    " You are Logged in as : ".$_SESSION['validuser']."<br /><br />";
            echo 
    "you will be transfered to your admin page in 5 seconds.";
            
    $validuser=TRUE;
            
    ?>
            <meta http-equiv="Refresh" content="3; URL=adminindex.php">
            <?php
        
    }
        else
        {
            
    ?>
            <form method="post" action="<?php echo $PHP_SELF ?>">
                <fieldset>
                    <legend>Login</legend>
                    <?php
                        
    echo ($msg_user)?"<br />".$msg_user."<br />":"";
                    
    ?>
                    <label>Username:
            
                    <input type="text" name="username" value="" size="25"/>
                    </label><br/>
                    <?php
                        
    echo ($msg_pass)?"<br />".$msg_pass."<br />":"";
                    
    ?>

                    <label>Password:
                        <input type="password" name="password" value="" size="25"/>
                    </label><br/>
                    <input type="submit" name="submit" value="Login">
                </fieldset>
            </form>
            <?php
            
    }
            
    ?>
    Thanks for any help.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what, specifically, makes you think that it doesnt work?

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I enter username/password and click login. Then everything goes blank. It works fine on a php5 server, just not php4....

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your error log and if that doesn't answer your question as to why it doesn't work, post the errors it's logging.

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add this to the top of your scripts, it may help you find your problem.

    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    i dont see any code that would be different between php4 and 5. however, both php4 and 5 can be configured with different settings. your code may rely upon specific settings, which means you could have problems even with php5 if php is not confugred how you need it.

  6. #6
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    Thanks for you replies.
    I have turnd on error reporting and I'm working through them. Im stuck on one error - it says

    You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1

    So i echoed the query, which is returning 'Resource id #3'.

    How can my origanal query
    ("SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'"); return 'Resource id #3'.? What does that mean and How can I fix it? I tired google but no help.

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont think that error is possible with the code you posted. have you changed it?

    echo your query
    PHP Code:
    echo $sql "SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'";
            
    $cQuery mysql_query($sql) or die(mysql_error()); 

  8. #8
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I have included the code you suggested to echo the query,
    Now the error has chagned to

    SELECT uname FROM gallery_users WHERE uname='tom' AND pass='test1'Unable to execute the query : from here You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1

    The updated version of the code I am currentlty working with is:
    PHP Code:
    <?php
        session_start
    ();
         
    ini_set('display_errors'1);
        
    error_reporting(E_ALL); 
        
    $msg_pass="";
        
    $msg_user="";
        
    $username=trim($_POST['username']);
        
    $password=trim($_POST['password']);
        if(
    $username && $password)
        {
            
    // connect to database
            
    include("includes/db_con1.php");
            
    // check if the username exists
            
    $cQuery mysql_query("SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'");
            echo 
    $sql "SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'";
            
    $cQuery mysql_query($sql) or die(mysql_error()); 
            
    $rs=mysql_query($cQuery);
            if(
    $rs)
            {
                
    $count=mysql_num_rows($rs);
                if(
    $count>0)
                {
                    
    //read the data from table
                    
    $data=mysql_fetch_assoc($rs);
                    if(
    strcmp($data['password'],$ushapass))
                    {
                        
    // setting a session with a variable named valid user which stores the username
                        
    $_SESSION['validuser']=$username;
                        
                    }
                    else
                    {
                        
    $msg_pass"Please check your username and password are correct";
                    }
                }
                else
                {
                    
    $msg_user="Please check your username and password are correct";
                }
            }
            else
            {
                echo 
    "Unable to execute the query : from here   ".mysql_error($con);
            }
        }
    // end of check
        
        
    if(isset($_SESSION['validuser']))
        {
            echo 
    " You are Logged in as : ".$_SESSION['validuser']."<br /><br />";
            echo 
    "you will be transfered to your admin page in 5 seconds.";
            
    $validuser=TRUE;
            
    ?>
            <meta http-equiv="Refresh" content="3; URL=adminindex.php">
            <?php
        
    }
        else
        {
            
    ?>
            <form method="post" action="<?php echo $PHP_SELF ?>">
                <fieldset>
                    <legend>Login</legend>
                    <?php
                        
    echo ($msg_user)?"<br />".$msg_user."<br />":"";
                    
    ?>
                    <label>Username:
            
                    <input type="text" name="username" value="" size="25"/>
                    </label><br/>
                    <?php
                        
    echo ($msg_pass)?"<br />".$msg_pass."<br />":"";
                    
    ?>

                    <label>Password:
                        <input type="password" name="password" value="" size="25"/>
                    </label><br/>
                    <input type="submit" name="submit" value="Login">
                </fieldset>
            </form>
            <?php
            
    }
            
    ?>

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see it now.

    $rs = mysql_query($sql);


    get rid of all other calls to mysql_query


    btw- i dont see how this code ran on php5 at all unless your first post was already a modified version.

  10. #10
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, managed to fix it. The error was in this part of the code:
    PHP Code:
    $cQuery "SELECT uname FROM gallery_users WHERE uname='$username' AND pass='$password'";
            
    $rs=mysql_query($cQuery);
            if(
    $rs
    Note the changes - no ()'s and no mysql_query...
    Thanks for the help tho.


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
  •