SitePoint Sponsor

User Tag List

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

    redirected page detect logged in user_id

    When a user login, he/she will be redirected to his/her page based on the user type. I've accomplished that.

    Right now what i need is to make the page recognize the user_id(auto-increment in user table) of the logged in user.

    function.php
    Code:
    <?php
    function checkUserType($type)
    {
    	switch($type)
    	{
    		case 'admin':
    		$page = 'admin.php';
    		break;
    		
    		case 'student':
    		$page = 'student.php';
    		break;
    		
    		case 'lecturer':
    		$page = 'lecturer.php';
    		break;
    		
    		case 'parents':
    		$page = 'parents.php';
    		break;
    	}
    	
    	header('Location: '.$page); //You may need to edit the path to suit your folder structure.
    	exit();
    }
    
    function checkUserStatus($actual_page)
    {
    	//First check to make sure the $_SESSION['type'] is set, if it's not then they haven't logged in so redirect back to login screen
    	if(!isset($_SESSION['type']))
    	{ header('Location: index.php'); }
    	else
    	{
    		if($actual_page == $_SESSION['type']){}
    		else{ checkUserType($_SESSION['type']); }
    	}
    }
    ?>
    index.php (login page)
    Code:
    <?php session_start(); ?>
    <html>
    	<head>
    		<title> Login Form </title>
    	</head>
    
    <body>
    <form method='post' action='index.php'>
    	<table width='400' border='5' align='center'>
    	<tr>
    		<td align='center'
    		colspan='5'><h1>Member Login</h1></td>
    	</tr>
    	
    	<tr>
    		<td align='center'>Username:</td>
    		<td><input type='text' name='username' /></td>
    	</tr>
    	
    	<tr>
    		<td align='center'>Password:</td>
    		<td><input type='password' name='pass' /></td>
    	</tr>
    	
    	<tr>
    		<td colspan='5' align='center'><input type='submit' name='login' value='Log In' /></td>
    	</tr>
    	
    	</table>
    </form>
    </body>
    </html>
    <?php
    mysql_connect("localhost","root","");
    mysql_select_db("class_attendance");
    
    if(isset($_POST['login'])){
            require_once('function.php');
    	$username = $_POST['username'];
    	$password = $_POST['pass'];
    	
    	$check_user = "select * from user where username='$username' AND pass='$password'";
    	
    	$run = mysql_query($check_user);
    	
    	if(mysql_num_rows($run)>0){
    	$results = mysql_fetch_assoc($run);
    		$_SESSION['user_id']=$id;
            $_SESSION['type'] = $results['type'];
    	checkUserType($results['type']);
    	}
    	else {
    	echo "<script>alert('Username or Password is incorrect!')</script>";
    	}
    }
    
    ?>
    student.php
    Code:
    <?php
    session_start();
    require_once('function.php'); //Set this to what ever page you include that holds all your functions so that we can use the checkUserStatus()
    checkUserStatus('student');
    ?>
    <!DOCTYPE html>
    <html>
    	<head>
    		<title> Student Page </title>
    	</head>
    
    <body>
    <h2 align='right'><a href='logout.php'>Logout</a></h2>
    </body>
    </html>

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    This line doesn't look right (in index.php):
    PHP Code:
    $_SESSION['user_id']=$id
    I think you want it to read
    PHP Code:
    $_SESSION['user_id']=$results['user_id']; 
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    As an aside:

    Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn't tie you down so much to a particular database server software.

    Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i fixed to this one..
    Code:
    $_SESSION['user_id']=$results['user_id'];
    but how to define it at function.php or student.php in order to grab the user_id from the index.php?

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    After index.php has been processed, you will use $_SESSION['user_id'] to get to it for the other pages.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm ok i added this part to the student.php..

    PHP Code:
    $studentId = !empty($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
     
    $studentInfo getStudentInfoFromId($studentId); 
    still not working.. do i need to add something to the function.php page or something?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Put var_dump($_SESSION); in your student.php and provide me with the output of that call. My guess is 'user_id' still isn't set. Did you logout and then log back in after making the changes to the login piece?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the error when i login..

    Code:
    Fatal error: Call to undefined function getStudentInfoFromId() in D:\wamp\www\class_attendance\student.php on line 7
    sorry im just learning php like 2 weeks ago, so kind of weak with calling functions and so on..

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Have you defined that function any where? Is it in your functions.php file or a different file?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no.. so i must define it 1st?

    if yes, where should i define it?

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r1nk View Post
    no.. so i must define it 1st?
    Yes, otherwise, you have nothing to call (hence the error)

    Quote Originally Posted by r1nk View Post
    if yes, where should i define it?
    That's really an organizational question that you could only answer. Being new to PHP, I'd be remiss not to suggest a few books that may help you with your endeavor into the language (and would teach you good practices in the process)
    https://learnable.com/books/php-mysq...ja-5th-edition
    https://learnable.com/books/jump-start-php

    Then there are these courses:
    https://learnable.com/courses/php-my...r-beginners-13
    https://learnable.com/courses/object-oriented-php-2734 (do this one after the php/mysql web development for beginners course)
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


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
  •