How do I hold a session through a PHP login redirect?

motoko96

I've been experimenting with php lately and I'm trying to understand sessions. So far I understand that each page needs a session_start() if I am to require a login to view certain pages/ carry information, but what I cannot figure out is how to keep the user's information after the login page.

Here is my login.php script:

<?php

session_start();

?>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<link href='http://fonts.googleapis.com/css?family=Lato|Quattrocento+Sans|Oxygen|Hind|Raleway' rel='stylesheet' type='text/css'>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="career.js"></script>
</head>
<body onload="fadeUp()">
<form id="login" class="login" action="logCheck.php" method="POST">
<div id="titleArea"><img src="tampa-bay.png" class="t2"><div class="workspaceTitle">CPC <br><align ="left">WorkSpace</align></div></div>
<center>
<table class="logTable">
<tr><td colspan="2"><input type="text" class="field" value="username" onclick='javascript: this.value = ""' name="user"></td></tr>
<tr><td colspan="2"><input type="password" class="field" onclick='javascript: this.value = ""'  value="password" name="pass"></td></tr>
<tr><td></td><td colspan="2"><input type="submit" name="login" value="Login"></td></tr>
<tr><td></td><td colspan="2">Don't have an account?<a href="register.php"> Register Here</a></td></tr>
<tr><td></td><td colspan="2">Forgot your username/password?<a href="register.php"> Click Here</a></td></tr>
</table>
</center>
</form>
</body>

And here is the relevant login verification (logCheck.php) script

if(isset($_POST['login'])){

    $username = mysqli_real_escape_string($con,$_POST['user']);

    $pass = mysqli_real_escape_string($con,$_POST['pass']);

    $sel_user = "select * from userdata where username='$username' AND password='$pass'";

    $run_user = mysqli_query($con, $sel_user);

    $check_user = mysqli_num_rows($run_user);

    if($check_user>0){
        $_SESSION['username']=$_POST['user'];

        echo "<script>window.open('careerindex.php','_self')</script>";
    }
    else {
        echo "<script>alert('Username or password is not correct, try again!')</script><script>window.open('login.php','_self')</script>";
    }
}
?>

Lastly, I also have an includes which holds the header on all pages that require logins. Here is the header's session:

<?php
session_start();
if (isset($_SESSION['username'])) {
?>
    logged in HTML and code here
<?php
} else {
?>
    Not logged in HTML and code here
<?php
}
?>

I've tried to use a regular session_Start() on each page, a session variable that uses the username (like $_SESSION['username']=$_POST['user'];). But I can't understand what I'm doing wrong. Any suggestions? (Ps, I've tried google searching, looking up answers here and w3 schools but none of the methods suggested seem to work)

Martin

Anywhere you are using any $_SESSION data, you need to initiate the session with session_start(), including in header and include files, you can find easily from StackOverflow various methods to check if a session has already been started and then if not, you can call session_start(); .

As long as you reference the $_SESSION values correctly and you have sorted out that session_start(); runs correctly, if you have further issues then you'd need to provide more specific details and code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I expire a PHP session after 30 minutes?

From Dev

how do I sent escape character ^] through a spawned telnet session?

From Dev

PHP how do I maintain my variables on a redirect to myself so that when I pass through I don't get a message when I hit back

From Dev

How do I stop cURL from deadlocking my PHP session?

From Dev

how to redirect session login url in moodle

From Dev

How to make entire page redirect to login after session expires in mvc?

From Dev

How do I destroy a specific session variable in PHP?

From Dev

How do I redirect Auth Login to on Admin/Login page?

From Dev

how do i redirect a table array to display its values in php

From Dev

How to redirect to login page if session is not available in MVC

From Dev

How do I redirect visitors to login page?

From Dev

how do i give session email to php?

From Dev

do I have to check login session when initiating PHP class?

From Dev

how can I redirect login page if do not login in yii framework?

From Dev

In liferay, how do I redirect to the login page and back?

From Dev

How do I stop cURL from deadlocking my PHP session?

From Dev

In Spring, how do I redirect the user to a different domain, while preserving their session?

From Dev

How do i change the header location in PHP to redirect to a subdomain?

From Dev

How do I do simple flat link to .php file redirect?

From Dev

How to hold session with ClamAV daemon?

From Dev

How do I redirect Auth Login to on Admin/Login page?

From Dev

How do I prevent PHP code executing after a header redirect?

From Dev

Android Facebook login with aws cognito wont hold login session?

From Dev

How to set session for login and logout in php?

From Dev

How can I hold data through multiple Web pages

From Dev

How do I use a PHP session to prevent duplicate form submissions?

From Dev

how do i redirect using ajax and php together?

From Dev

PHP - How to check users level into a login session

From Dev

How can I redirect to a PHP page with an <a> tag? Also how can I pass an ID number through the URL with it?

Related Related

  1. 1

    How do I expire a PHP session after 30 minutes?

  2. 2

    how do I sent escape character ^] through a spawned telnet session?

  3. 3

    PHP how do I maintain my variables on a redirect to myself so that when I pass through I don't get a message when I hit back

  4. 4

    How do I stop cURL from deadlocking my PHP session?

  5. 5

    how to redirect session login url in moodle

  6. 6

    How to make entire page redirect to login after session expires in mvc?

  7. 7

    How do I destroy a specific session variable in PHP?

  8. 8

    How do I redirect Auth Login to on Admin/Login page?

  9. 9

    how do i redirect a table array to display its values in php

  10. 10

    How to redirect to login page if session is not available in MVC

  11. 11

    How do I redirect visitors to login page?

  12. 12

    how do i give session email to php?

  13. 13

    do I have to check login session when initiating PHP class?

  14. 14

    how can I redirect login page if do not login in yii framework?

  15. 15

    In liferay, how do I redirect to the login page and back?

  16. 16

    How do I stop cURL from deadlocking my PHP session?

  17. 17

    In Spring, how do I redirect the user to a different domain, while preserving their session?

  18. 18

    How do i change the header location in PHP to redirect to a subdomain?

  19. 19

    How do I do simple flat link to .php file redirect?

  20. 20

    How to hold session with ClamAV daemon?

  21. 21

    How do I redirect Auth Login to on Admin/Login page?

  22. 22

    How do I prevent PHP code executing after a header redirect?

  23. 23

    Android Facebook login with aws cognito wont hold login session?

  24. 24

    How to set session for login and logout in php?

  25. 25

    How can I hold data through multiple Web pages

  26. 26

    How do I use a PHP session to prevent duplicate form submissions?

  27. 27

    how do i redirect using ajax and php together?

  28. 28

    PHP - How to check users level into a login session

  29. 29

    How can I redirect to a PHP page with an <a> tag? Also how can I pass an ID number through the URL with it?

HotTag

Archive