Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V  < 1 2  
Reply to this topicStart new topic
> [PHP][mysql]Sprawdzania czy user jest Adminem
matius71
post 25.11.2017, 13:50:06
Post #21





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


<?php
$userlogin = $_SESSION['userlogin'];
$stmt = $db->query("SELECT memberID, userlogin='$userlogin', permissions FROM members");
echo '<ul>';
foreach($stmt as $row)
{
echo '<li>'.$row['memberID'].') '.$row['userlogin'].': '.$row['permissions'].'</li>';
}
$stmt->closeCursor();
echo '</ul>';

?>

ale wywala to co zwykle czyli:

Notice: Undefined index: userlogin in D:\XAMPP\htdocs\memberpage.php on line 39
33) : Administrator

linia 39 to
CODE
echo '<li>'.$row['memberID'].') '.$row['userlogin'].': '.$row['permissions'].'</li>';

Go to the top of the page
+Quote Post
viking
post 25.11.2017, 13:58:12
Post #22





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Nie wiem ile razy można ci powtarzać w tym temacie że masz startować sesję zanim z niej skorzystasz.
Zapytanie też pokazałem jak ma wyglądać.


--------------------
Go to the top of the page
+Quote Post
matius71
post 25.11.2017, 14:03:29
Post #23





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


Ok, rozumiem że chodzi ci o to żeby dodać na początku session start

CODE
<?php
session_start();
$memberID=$_SESSION['memberID'];
$stmt = $db->query("SELECT memberID='$memberID', userlogin, permissions FROM members");
echo '<ul>';
foreach($stmt as $row)
{
echo '<li>'.$row['memberID'].') '.$row['userlogin'].': '.$row['permissions'].'</li>';
}
$stmt->closeCursor();
echo '</ul>';

?>


Nie ważne w którym miejscu dodam to i tak wywala błąd pod tytułem:
CODE
Notice: A session had already been started - ignoring session_start() in D:\XAMPP\htdocs\memberpage.php on line 34
Go to the top of the page
+Quote Post
viking
post 25.11.2017, 14:06:31
Post #24





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


var_dump($_SESSION); przed $memberID


--------------------
Go to the top of the page
+Quote Post
matius71
post 25.11.2017, 14:10:16
Post #25





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


CODE
array(3) { ["loggedin"]=> bool(true) ["userlogin"]=> string(6) "tester" ["memberID"]=> int(1) }
Go to the top of the page
+Quote Post
viking
post 25.11.2017, 14:13:54
Post #26





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Popraw zapytanie, później iteruj p $stmt->fetch


--------------------
Go to the top of the page
+Quote Post
matius71
post 25.11.2017, 15:02:38
Post #27





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


Nie wiem czy to o coś takiego chodzi?

CODE
<?php

$memberID=$_SESSION['memberID'];
$stmt = $db->query("SELECT memberID='$memberID', userlogin, permissions FROM members");
$stmt->execute(array(':memberID' => $memberID));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

echo '<ul>';
foreach($stmt as $row)
{
echo '<li>'.$row['memberID'].') '.$row['userlogin'].': '.$row['permissions'].'</li>';
}
$stmt->closeCursor();
echo '</ul>';
?>


Nie wiem co mam poprawić w zapytaniu.

Ten post edytował matius71 25.11.2017, 15:03:47
Go to the top of the page
+Quote Post
viking
post 25.11.2017, 15:04:34
Post #28





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Pocztaj trochę dokumentację mysql. Co to memberID='$memberID' niby ma robić? Podałem jak masz zmienić warunek where.


--------------------
Go to the top of the page
+Quote Post
matius71
post 27.11.2017, 16:12:40
Post #29





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


Dobra, już mi działa:

CODE
$memberID=$_SESSION['memberID'];
$stmt = $db->query("SELECT memberID, userlogin, permissions FROM members where memberID='$memberID'");
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$_SESSION['permissions'] = $row['permissions'];


Teraz chciałem zrobić niby proste sprawdzenie ale oczywiście nie działa, zawsze wyświetla "Masz uprawnienia SeniorAdmin lub Administrator." Co tu jest nie tak?

CODE
if ($_SESSION['permissions'] == ("SeniorAdmin" || "Administrator"))
{
echo '<p>Masz uprawnienia SeniorAdmin lub Administrator.</p>';

require('admin/addnew.php') ;

} else
{
echo '<p>Nie masz uprawnien SeniorAdmin lub Administrator.</p>';
}


Dodam że wszystko działa jeśli sprawdzam czy user ma uprawnienia tylko np "SeniorAdmin" natomiast jak dodaje "|| Administrator" to już przestaje działać thumbsdownsmileyanim.gif
Go to the top of the page
+Quote Post
nospor
post 27.11.2017, 16:14:23
Post #30





Grupa: Moderatorzy
Postów: 36 455
Pomógł: 6292
Dołączył: 27.12.2004




zacznij prosze uzywac poprawnego bbcode dla kodu wstawianego.

A to:
if ($_SESSION['permissions'] == ("SeniorAdmin" || "Administrator"))
to zes skad wytrzasnal? Nigdzie nie ma takiej kontrukcji...

  1. if ($_SESSION['permissions'] === "SeniorAdmin" || $_SESSION['permissions'] === "Administrator")


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
matius71
post 27.11.2017, 16:23:42
Post #31





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 22.08.2009

Ostrzeżenie: (0%)
-----


Czyli za każdym razem trzeba $_SESSION['permissions'] ? Nie da się jakoś krócej? biggrin.gif Rzeczywiście, to co podałeś działa pięknie!
Go to the top of the page
+Quote Post
Pyton_000
post 27.11.2017, 16:25:37
Post #32





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Kod
if (in_array($_SESSION['permissions'], ["SeniorAdmin", "Administrator"]))

Albo tak
Go to the top of the page
+Quote Post

2 Stron V  < 1 2
Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 23.04.2024 - 22:47