Jump to content

Help with sign in and session please


1acid

Recommended Posts

Hi I am making a sign in page for a simple web app.

 

I have user data entered into my DB already that you could use to sign in.

 

 

here is my code

 

<?php
        session_start();
        include("functions.php");
        include("config.php");
        include("connect.php");
    
        $error=array();
        
        if(isset($_POST['signin']))
        {
            if(empty($_POST['username']))
            {
                $error['username'] = "Username PLZ!";
            }
            if(empty($_POST['password']))
            {
                $error['password'] = "PSWD PLEASE!";
            }
            if(count($error)==0)
            {
                $cleanData =sanitize_array($_POST);
                $pswd = md5($cleanData['password']);
                
                $sqlStr = "SELECT id FROM calcuser WHERE username='{$cleanData['username']}'AND password='{$pswd}'";
            
                $result = mysql_fetch_array(mysql_query($sqlStr));
                
                if($result)
                {
                    $_SESSION['$user_id']= $result['id'];
                    
                    redirect("index.php");
                }
                else
                 {
                $error['combi'] ="Password/username do not match records";
                 }
            }
            
        }
        ?>

 

and this is my code on the redirect page that should be getting the userid from the DB and matching it with the session id user_id

 

session_start();   
        include("functions.php");
        include("config.php");
        include("connect.php");
        
        
        if(!isset($_SESSION['user_id']))
        {
           redirect("signin.php");
        }
        $sqlStr = "SELECT id, name, username FROM calcuser WHERE id='{$_SESSION['user_id']}'";
        
        $row =(mysql_fetch_assoc(mysql_query($sqlStr)));

 

 

its not taking me to the index page, which to me means there is something wrong with either the sql query to get the user id from the DB

 

Any help would be greatly appreciated. Im pretty newb at all this.

 

Thanks(Man i hope these code brackets worked!!)

 

Link to comment
https://forums.phpfreaks.com/topic/266173-help-with-sign-in-and-session-please/
Share on other sites

Your code probably is going to the index.php page, but it is redirecting back to the signin.php page.

 

The session variable name you are setting is - $_SESSION['$user_id']. You probably intended that to be $_SESSION['user_id']

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.