Hoe echo ik de gebruikersnaam van de huidige gebruiker die is ingelogd
Ik ben aan het werken met een inlog scriptje en ik wil graag de username retrieven van de huidige ingelogde gebruiker, voor in een ander deel van de website. Hier wil ik een aantal velden weergeven. Wat is hier de beste manier voor?
Kan ik iets van
toevoegen aan mijn inlog script?
En dan via:
De variabele definiëren.
Het huidige inlogscript:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<div class="container">
<div class="row">
<div class="col-md-5 center-block-e">
<div class="login-form">
<div class="login-form-inner">
<?php $gl = $this->session->flashdata('globalmsg'); ?>
<?php if(!empty($gl)) :?>
<div class="alert alert-success"><b><span class="glyphicon glyphicon-ok"></span></b> <?php echo $this->session->flashdata('globalmsg') ?></div>
<?php endif; ?>
<p class="login-form-intro"><img src="<?php echo base_url() ?>images/ava2.png" width="100"></p>
<?php if(isset($_GET['redirect'])) : ?>
<?php echo form_open(site_url("login/pro/" . urlencode($_GET['redirect'])), array("id" => "login_form")) ?>
<?php else : ?>
<?php echo form_open(site_url("login/pro"), array("id" => "login_form")) ?>
<?php endif; ?>
<div class="form-group login-form-area has-feedback">
<input type="text" class="form-control" name="email" placeholder="<?php echo lang("ctn_303") ?>">
<i class="glyphicon glyphicon-user form-control-feedback login-icon-color"></i>
</div>
<div class="form-group login-form-area has-feedback">
<input type="password" name="pass" class="form-control" placeholder="*********">
<i class="glyphicon glyphicon-lock form-control-feedback login-icon-color"></i>
</div>
<p><input type="submit" class="btn btn-flat-login form-control" value="<?php echo lang("ctn_150") ?>"></p>
<p class="decent-margin small-text"><a href="<?php echo site_url("login/forgotpw") ?>"><?php echo lang("ctn_181") ?></a> <span class="pull-right"><a href="<?php echo site_url("register") ?>"><?php echo lang("ctn_151") ?></a></span></p>
</div>
<div class="login-form-bottom clearfix">
<?php if(!$this->settings->info->disable_social_login) : ?>
<div class="text-center decent-margin-top">
<?php if(!empty($this->settings->info->twitter_consumer_key) && !empty($this->settings->info->twitter_consumer_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/twitter_login") ?>" class="btn btn-flat-social-twitter" >
<img src="<?php echo base_url() ?>images/social/twitter.png" height="20" class='social-icon' />
Twitter</a>
</div>
<?php endif; ?>
<?php if(!empty($this->settings->info->facebook_app_id) && !empty($this->settings->info->facebook_app_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/facebook_login") ?>" class="btn btn-flat-social-facebook" >
<img src="<?php echo base_url() ?>images/social/facebook.png" height="20" class='social-icon' />
Facebook</a>
</div>
<?php endif; ?>
<?php if(!empty($this->settings->info->google_client_id) && !empty($this->settings->info->google_client_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/google_login") ?>" class="btn btn-flat-social-google" >
<img src="<?php echo base_url() ?>images/social/google.png" height="20" class='social-icon' />
Google</a>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<hr>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
<div class="login-footer">
</div>
<script type="text/javascript">
$(document).ready(function() {
var form = "login_form";
$('#'+form + ' input').on("focus", function(e) {
clearerrors();
});
$('#'+form).on("submit", function(e) {
e.preventDefault();
// Ajax check
var data = $(this).serialize();
$.ajax({
url : global_base_url + "login/ajax_check_login",
type : 'POST',
data : {
formData : data,
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash() ?>'
},
dataType: 'JSON',
success: function(data) {
if(data.error) {
$('#'+form).prepend('<div class="form-error">'+data.error_msg+'</div>');
}
if(data.success) {
// allow form submit
$('#'+form+ ' input[type="submit"]').val("Logging In ...");
$('#'+form).unbind('submit').submit();
}
if(data.field_errors) {
var errors = data.fieldErrors;
console.log(errors);
for (var property in errors) {
if (errors.hasOwnProperty(property)) {
// Find form name
var field_name = '#' + form + ' input[name="'+property+'"]';
$(field_name).addClass("errorField");
// Get input group of field
$('#'+form).prepend('<div class="form-error">'+errors[property]+'</div>');
}
}
}
}
});
return false;
});
});
function clearerrors()
{
console.log("Called");
$('.form-error').remove();
$('.errorField').removeClass('errorField');
}
</script>
<div class="row">
<div class="col-md-5 center-block-e">
<div class="login-form">
<div class="login-form-inner">
<?php $gl = $this->session->flashdata('globalmsg'); ?>
<?php if(!empty($gl)) :?>
<div class="alert alert-success"><b><span class="glyphicon glyphicon-ok"></span></b> <?php echo $this->session->flashdata('globalmsg') ?></div>
<?php endif; ?>
<p class="login-form-intro"><img src="<?php echo base_url() ?>images/ava2.png" width="100"></p>
<?php if(isset($_GET['redirect'])) : ?>
<?php echo form_open(site_url("login/pro/" . urlencode($_GET['redirect'])), array("id" => "login_form")) ?>
<?php else : ?>
<?php echo form_open(site_url("login/pro"), array("id" => "login_form")) ?>
<?php endif; ?>
<div class="form-group login-form-area has-feedback">
<input type="text" class="form-control" name="email" placeholder="<?php echo lang("ctn_303") ?>">
<i class="glyphicon glyphicon-user form-control-feedback login-icon-color"></i>
</div>
<div class="form-group login-form-area has-feedback">
<input type="password" name="pass" class="form-control" placeholder="*********">
<i class="glyphicon glyphicon-lock form-control-feedback login-icon-color"></i>
</div>
<p><input type="submit" class="btn btn-flat-login form-control" value="<?php echo lang("ctn_150") ?>"></p>
<p class="decent-margin small-text"><a href="<?php echo site_url("login/forgotpw") ?>"><?php echo lang("ctn_181") ?></a> <span class="pull-right"><a href="<?php echo site_url("register") ?>"><?php echo lang("ctn_151") ?></a></span></p>
</div>
<div class="login-form-bottom clearfix">
<?php if(!$this->settings->info->disable_social_login) : ?>
<div class="text-center decent-margin-top">
<?php if(!empty($this->settings->info->twitter_consumer_key) && !empty($this->settings->info->twitter_consumer_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/twitter_login") ?>" class="btn btn-flat-social-twitter" >
<img src="<?php echo base_url() ?>images/social/twitter.png" height="20" class='social-icon' />
Twitter</a>
</div>
<?php endif; ?>
<?php if(!empty($this->settings->info->facebook_app_id) && !empty($this->settings->info->facebook_app_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/facebook_login") ?>" class="btn btn-flat-social-facebook" >
<img src="<?php echo base_url() ?>images/social/facebook.png" height="20" class='social-icon' />
Facebook</a>
</div>
<?php endif; ?>
<?php if(!empty($this->settings->info->google_client_id) && !empty($this->settings->info->google_client_secret)) : ?>
<div class="btn-group">
<a href="<?php echo site_url("login/google_login") ?>" class="btn btn-flat-social-google" >
<img src="<?php echo base_url() ?>images/social/google.png" height="20" class='social-icon' />
Google</a>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<hr>
<?php echo form_close() ?>
</div>
</div>
</div>
</div>
</div>
<div class="login-footer">
</div>
<script type="text/javascript">
$(document).ready(function() {
var form = "login_form";
$('#'+form + ' input').on("focus", function(e) {
clearerrors();
});
$('#'+form).on("submit", function(e) {
e.preventDefault();
// Ajax check
var data = $(this).serialize();
$.ajax({
url : global_base_url + "login/ajax_check_login",
type : 'POST',
data : {
formData : data,
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash() ?>'
},
dataType: 'JSON',
success: function(data) {
if(data.error) {
$('#'+form).prepend('<div class="form-error">'+data.error_msg+'</div>');
}
if(data.success) {
// allow form submit
$('#'+form+ ' input[type="submit"]').val("Logging In ...");
$('#'+form).unbind('submit').submit();
}
if(data.field_errors) {
var errors = data.fieldErrors;
console.log(errors);
for (var property in errors) {
if (errors.hasOwnProperty(property)) {
// Find form name
var field_name = '#' + form + ' input[name="'+property+'"]';
$(field_name).addClass("errorField");
// Get input group of field
$('#'+form).prepend('<div class="form-error">'+errors[property]+'</div>');
}
}
}
}
});
return false;
});
});
function clearerrors()
{
console.log("Called");
$('.form-error').remove();
$('.errorField').removeClass('errorField');
}
</script>
Iemand een idee?
Maar gezien je een framework lijkt te gebruiken, raad ik aan om in de documentatie te kijken hoe die met sessies omgaat.
probeer toe te voegen aan mijn inlogscript krijg ik:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: userdata
Filename: login/index.php
Line Number: 10
Backtrace:
File: /home/wcx/domains/webconexus.nl/public_html/my/application/views/login/index.php
Line: 10
Function: _error_handler
File: /home/wcx/domains/webconexus.nl/public_html/my/application/libraries/Template.php
Line: 33
Function: view
File: /home/wcx/domains/webconexus.nl/public_html/my/application/controllers/Login.php
Line: 31
Function: loadContent
File: /home/wcx/domains/webconexus.nl/public_html/my/index.php
Line: 315
Function: require_once
Ja precies, dit is een user management dashboard gemaakt met CodeIgniter. Alleen wanneer ik A PHP Error was encountered
Severity: Notice
Message: Undefined variable: userdata
Filename: login/index.php
Line Number: 10
Backtrace:
File: /home/wcx/domains/webconexus.nl/public_html/my/application/views/login/index.php
Line: 10
Function: _error_handler
File: /home/wcx/domains/webconexus.nl/public_html/my/application/libraries/Template.php
Line: 33
Function: view
File: /home/wcx/domains/webconexus.nl/public_html/my/application/controllers/Login.php
Line: 31
Function: loadContent
File: /home/wcx/domains/webconexus.nl/public_html/my/index.php
Line: 315
Function: require_once
Gewijzigd op 16/12/2018 13:29:09 door Raf Rasenberg
Bekijk ook even dit: https://www.codeigniter.com/user_guide/libraries/sessions.html
Super dankjewel Arien! Ik ga dit even doornemen. Fijn weekend!
Succes! :-)
Toepasbaar over de gehele website zonder extra gebruik van controllers o.i.d
Het zou inderdaad logischer zijn dat je een user object hebt die elke page-refresh wordt geinitialiseerd met minimale informatie uit de sessie (via een user id ofzo). Een user id is -qua gebruikersinformatie- ook eigenlijk het enige wat in een sessie bijgehouden hoeft te / zou moeten worden.